YAFLogo

webmix
  • webmix
  • 53% (Neutral)
  • YAF Forumling Topic Starter
11 years ago
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
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 3.0.3
11 years ago
You dont need to store the session. You can grab the current UserId directlly from yaf

YafContext.Current.PageUserID
webmix
  • webmix
  • 53% (Neutral)
  • YAF Forumling Topic Starter
11 years ago
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
11 years ago
Maybe just post your page on here? Looks like it should be working.
webmix
  • webmix
  • 53% (Neutral)
  • YAF Forumling Topic Starter
11 years ago
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
11 years ago
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
11 years ago
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
  • tha_watcha
  • 100% (Exalted)
  • YAF.NET Project Lead 🤴 YAF Version: 3.0.3
11 years ago
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();
        }
webmix
  • webmix
  • 53% (Neutral)
  • YAF Forumling Topic Starter
11 years ago
Thank you for bearing with me and assisting in helping me do this.
Chetan Bavarva
7 years ago
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
YAF Logo Copyright © YetAnotherForum.NET & Ingo Herbote. All rights reserved
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