Link to home
Start Free TrialLog in
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?


}
-------------------------------------------
Avatar of nauman_ahmed
nauman_ahmed
Flag of United States of America image

What do you mean by identity?

--Nauman.
Avatar of fwsteal
fwsteal

ASKER

domain name/ userid
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.
Avatar of 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
Avatar of nauman_ahmed
nauman_ahmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial