webmix
  •  webmix
  • 53% (Neutral)
  • YAF Forumling Topic Starter
2012-01-07T15:43:06Z
Hello

This is first time I am using the ASP.NET membership/login feature, I ask you to please bear with me.

This is what I am trying to achieve:
I have created a custom page and the login membership works, from YAF.
When the page opens I want the UserID placed in session so that I can use it to fill columns/tables and access specific data for one person.

After trying to get this to work for 12 hours my brain is fried and I would greatly appreciate some help as I am now going round in circles.

This is the code I have in the page behind.

Without laughing to much can someone please help.

Thank you.


using YAF.Classes;
using YAF.Core;
using YAF.Core.Services;
using YAF.Types.Interfaces;
using YAF.Types.Constants;
using YAF.Classes.Data;
using YAF.Utils;
using YAF.Utils.Helpers;
using YAF.Utils.Helpers.StringUtils;
using YAF.Utils.Structures;
using YAF.Types.Constants;
using YAF.Types.Interfaces;
using System.Web.UI.WebControls;

public partial class Forum_test : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

private object userNameToLookup;
    protected void Page_Init(object sender, EventArgs e)
    {
            
        var UserId = UserMembershipHelper.GetUserIDFromProviderUserKey(Membership.GetUser(userNameToLookup).ProviderUserKey);
        UserId = Convert.ToInt32(User.Text);
        User.Text = (string)(Session["UserId"]);
        if (UserId != null)
        {
            Response.Redirect("~/yaf_login.aspx");
        }
    }

    public string UserId { get; set; }

}
Sponsor
tha_watcha
2012-01-07T17:14:24Z
You dont need to store the session. You can grab the current UserId directlly from yaf

YafContext.Current.PageUserID

UserPostedImage
webmix
  •  webmix
  • 53% (Neutral)
  • YAF Forumling Topic Starter
2012-01-09T15:20:02Z
Hallo, ich danke Ihnen für Ihre Hilfe.

Hello, thank you for your help.

I am getting data, but it is not what I am looking for. Currently the request returns a single number, in this case the number 8.

Again I have tried all I know, here is the code I am using in the page behind to get the value of the UserID.

protected void Page_Load(object sender, EventArgs e)
{
HiddenField UserId3 = (HiddenField)FormView3.Row.FindControl("UserId3");
UserId3.Value = YafContext.Current.PageUserID.ToString();

Any suggestions where I am going wrong?

Thanks again
Till later
Michael
Jaben
  •  Jaben
  • 100% (Exalted)
  • YAF Developer
2012-01-09T18:56:15Z
Maybe just post your page on here? Looks like it should be working.
webmix
  •  webmix
  • 53% (Neutral)
  • YAF Forumling Topic Starter
2012-01-10T17:26:25Z
Thanks, here is the code, I have taken it down to showing just the UserID in a label.
To see it work I created a test account in the forum:
http://www.forum.aysa.org.za/ 
Username: test - password test01
Then access the page
http://www.forum.aysa.org.za/test2.aspx 

Page Code

<%@ Page Title="Test page" Language="C#" MasterPageFile="~/AnglicanYouth.master" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="Forum_test" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Panel ID="Panel3" runat="server" CssClass="panel-body">
        This is the User Name =
        <asp:LoginName ID="LoginName1" runat="server" />
        <br />
        This is the User ID =
        <asp:Label ID="UserIDLabel2" runat="server" Text="Label"></asp:Label>
        <br />
    </asp:Panel>
</asp:Content>


Page Behind Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using YAF.Core;

public partial class Forum_test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UserIDLabel2.Text = YafContext.Current.PageUserID.ToString();
    }
}
Jaben
  •  Jaben
  • 100% (Exalted)
  • YAF Developer
2012-01-10T19:38:24Z
Honestly, you were closer before...


var membershipUser = Membership.GetUser();

if (membershipUser != null)
{
  int userId = UserMembershipHelper.GetUserIDFromProviderUserKey(membershipUser.ProviderUserKey);
}


That should be the current user id.

YafContext.Current.PageUserID requires a pageload -- which is not needed in this situation.
webmix
  •  webmix
  • 53% (Neutral)
  • YAF Forumling Topic Starter
2012-01-10T19:54:09Z
It's still returning the single digit,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using YAF.Core;

public partial class Forum_test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var membershipUser = Membership.GetUser();

        if (membershipUser != null)
        {
            int userId = UserMembershipHelper.GetUserIDFromProviderUserKey(membershipUser.ProviderUserKey);
            UserIDLabel2.Text = userId.ToString();
        }
    }
}
tha_watcha
2012-01-11T11:58:42Z
I don't understand your Problem you want to retrieve the UserID and thats whats the code is producing. Or are you trying to retrieve the Membership Provider UserID, if thats the case you only need to...

var membershipUser = Membership.GetUser();

        if (membershipUser != null)
        {
            var userId = membershipUser.ProviderUserKey;
            UserIDLabel2.Text = userId.ToString();
        }

UserPostedImage
webmix
  •  webmix
  • 53% (Neutral)
  • YAF Forumling Topic Starter
2012-01-11T13:58:18Z
Thank you for bearing with me and assisting in helping me do this.
Chetan Bavarva
2016-11-23T06:36:34Z
Here is the solution:


protected void Page_Load(object sender, EventArgs e)
{
   System.Web.Security.MembershipUser mu = System.Web.Security.Membership.GetUser();
   string strUsrId = mu.ProviderUserKey.ToString();       
}

Chetan Patel
Custom software company India 
iFour Technolab Pvt. Ltd

About Us

The YAF.NET is an open source .NET forum project. YAF.NET is supported by an team of international developers who are build community by building community software.

Powered by Resharper Donate with PayPal button

Project Twitter Updates

Copyright © YetAnotherForum.NET & Ingo Herbote. All rights reserved