System.Security.Principal.WindowsIdentity.GetCurrent (). Name works

I am using System.Security.Principal.WindowsIdentity.GetCurrent().Name

(with windows authentication in my web.config) to get the client's username.

My question is if this code works when I put the page on the intranet, because I need to store the username in the session for later use.

Can I get the username that the user used to login to my page using this code?

void Authenticate()
{
    string strLogin = null;

    try
    {
        strLogin = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        Session.Add("Login", strLogin);
        //Label1.Text = strLogin;
        //Label2.Text = System.Environment.UserName;
    }
    catch (Exception ex)
    {
        Response.Write(strLogin);
        Response.Write("<br/>handled:" + ex.Message.ToString());
        Response.End();
    }
    finally
    {
        GC.Collect();
    }
}

      

+1


source to share


3 answers


You will need to configure your application to impersonate the Windows user that is accessing it by adding the following element after the authentication element in Web.Config:

<identity impersonate="true"/>

      



If you don't set impersonation to true, the worker process runs as a network or ASPNET account, depending on your version of IIS.

+4


source


Yes. and it also works in Page.User.Identity.Name



+1


source


If your users visit your site, you can get the name of the current user with the name Page.User.Identity.Name.

0


source







All Articles