Avatar of fwsteal
fwsteal
 asked on

asp.net 2.0 c# get current user's identity

How should I get the current user's identity via the class file below?


-------------------------------------------
welcome.ascx control contents:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Net;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Principal;
using System.IO;

public partial class welcome : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // get current user's identity
            WindowsIdentity wi = WindowsIdentity.GetCurrent();
            string accountName = wi.Name.ToString();
            string[] values = accountName.Split(@"\".ToCharArray()); //split it
            string MyDomain = values[0]; //domain
            string MyUserID = values[1]; //userid

            //get user's FullName
            LabelFullName.Text = " " + UserInfo.GetUserFullName(MyUserID).FullName.ToString();
        }
    }
}
-------------------------------------------
app_code/bll/clsUserInfo.cs contents:

using System;
using System.Data;
using System.Data.Common;
using System.Security.Principal;
using System.DirectoryServices;

public struct UserFullName
{
    public string FullName;
}

public static class UserInfo
 {
      static UserInfo()
      {

      }

    //get user full name
    public static UserFullName GetUserFullName(string UserID)
    {
        // get a configured DbCommand object
        DbCommand comm = GenericDataAccess.CreateCommand();
        // set the stored procedure name
        comm.CommandText = "usp_GetUserFullName";
        // create a new parameter
        DbParameter param = comm.CreateParameter();
        param.ParameterName = "@email_id";
        param.Value = UserID;
        param.DbType = DbType.String; //varchar;
        comm.Parameters.Add(param);
        // execute the stored procedure
        DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
        // wrap retrieved data into a UserDetails object
        UserFullName details = new UserFullName();
        if (table.Rows.Count > 0)
        {
            details.FullName = table.Rows[0]["FullName"].ToString();
        }
        else
        {
            details.FullName = "N/A";
        }
        // return user details
        return details;
    }


// how to get current user's identity?


}
-------------------------------------------
ASP.NET

Avatar of undefined
Last Comment
nauman_ahmed

8/22/2022 - Mon
nauman_ahmed

What do you mean by identity?

--Nauman.
fwsteal

ASKER
domain name/ userid
nauman_ahmed

Use the code from user control :)


WindowsIdentity wi = WindowsIdentity.GetCurrent();
string accountName = wi.Name.ToString();
string[] values = accountName.Split(@"\".ToCharArray()); //split it
string MyDomain = values[0]; //domain
string MyUserID = values[1]; //userid

--Nauman.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
fwsteal

ASKER
I'm doing that but I am trying to figure out how to put it into the class file so I can use it else where as needed.
ASKER CERTIFIED SOLUTION
nauman_ahmed

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question