【问题标题】:Unable to get the current user identity value无法获取当前用户身份值
【发布时间】:2018-02-09 12:05:18
【问题描述】:

我有一个 Intranet 网站,当我在本地机器上运行时,我可以获取用户的身份值。

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

但是当我在 IIS 8.5 上部署它时,它发现它是空白的。

请帮助我了解我哪里出错了?

对于模拟,我们必须使用特定的用户名和密码。

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 设置:

Windows authentication - Enabled
Impersonation  - Enabled
Rest all disabled.

Default app pool - Integrated mode.

【问题讨论】:

    标签: c# asp.net iis iis-8


    【解决方案1】:

    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  ---+
    

    使用User.Identity.Name获取登录用户和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.";
        }
    }
    

    【讨论】:

      【解决方案2】:

      使用HttpContext.Current.User.Identity 类获取当前用户的完整信息。

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

      【讨论】:

        【解决方案3】:

        这将显示当前用户的身份。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-14
          • 2014-06-28
          • 2023-03-13
          • 1970-01-01
          相关资源
          最近更新 更多