简体   繁体   English

无法获取当前用户标识值

[英]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. 我有一个Intranet网站,我可以在本地机器上运行时获取用户的身份值。

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

But When I deploy the same on IIS 8.5, It finds it blank. 但是当我在IIS 8.5上部署它时,它发现它是空白的。

Please help me understand where I am going wrong ? 请帮我理解我哪里错了?

For impersonation we have to use the specific username and password. 对于模拟,我们必须使用特定的用户名和密码。

Web.config: 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: IIS设置:

Windows authentication - Enabled
Impersonation  - Enabled
Rest all disabled.

Default app pool - Integrated mode.

IIS settings should be like this: IIS设置应如下所示:

+------------------------------------+
|           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 : 使用User.Identity.Name获取登录用户并进行如下测试

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. 使用HttpContext.Current.User.Identity类获取当前用户的完整信息。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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