简体   繁体   中英

Unable to get the current user identity value

I have a intranet website where I am able to get the user's identity value when run in Local machine.

System.Web.HttpContext.Current.User.Identity.Name.ToString().Substring(3)

But When I deploy the same on IIS 8.5, It finds it blank.

Please help me understand where I am going wrong ?

For impersonation we have to use the specific username and password.

Web.config:

<system.web>
    <authentication mode="Windows" />
        <customErrors mode="RemoteOnly" defaultRedirect="~/Pages/Error/Error.aspx" >
            <error statusCode="404" redirect="~/Pages/Error/404.aspx" />
        </customErrors>
    <identity impersonate="true" userName="user1" password="pass1" />
  </system.web>
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <validation validateIntegratedModeConfiguration="false" />
</system.webServer>

IIS settings:

Windows authentication - Enabled
Impersonation  - Enabled
Rest all disabled.

Default app pool - Integrated mode.

IIS settings should be like this:

+------------------------------------+
|           Authentication           |
+------------------------------------+
Name                         Status
------------------------     --------
Anonymous Authentication     Disabled ---+
ASP.NET Impersonation        Enabled     |
Basic Authentication         Enabled     |__ Both are important
Digest Authentication        Disabled    |
Forms Authentication         Disabled    |
Windows Authentication       Enabled  ---+

Use User.Identity.Name to get the logon user and test like this :

protected void Page_Load(object sender, EventArgs e)
{
    if (User.Identity.IsAuthenticated)
    {
        Page.Title = "Home page for " + User.Identity.Name;
    }
    else
    {
        Page.Title = "Home page for guest user.";
    }
}

Use HttpContext.Current.User.Identity class to get full information current user.

If HttpContext.Current.User.Identity.IsAuthenticated Then
        currentUserID = UserInfo.UserID
End If

This will display the current users identity.

            int idx;
            String strID;
            strID = WindowsIdentity.GetCurrent().Name;
            strID = strID.Replace('/', '\\');
            idx = strID.IndexOf('\\');
            if (idx != 0)
                strID = strID.Substring(idx + 1).ToLower();
            MessageBox.Show(""+strID);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM