【问题标题】:Getting Windows User ID When Using Forms Authentication使用表单身份验证时获取 Windows 用户 ID
【发布时间】:2013-04-20 19:38:24
【问题描述】:

我正在开发在我们的 Intranet 上运行的 ASP.NET MVC 应用程序。我们正在使用表单身份验证,但是设置的用户名将与 Active Directory 用户名相同。我正在尝试找到一种使用 Windows 用户名预填充登录用户名字段的方法,但找不到访问当前 Windows 用户身份的方法。

System.Environment.UserDomainName 返回“IIS APPPOOL”

System.Security.Principal.WindowsPrincipal.Current.Identity 返回一个 GenericIdentity

HttpContext.Request.LogonUserIdentity.Name 返回“NT AUTHORITY\IUSR”

我不想验证 Windows 用户或以 Windows 用户身份登录网站,我只想读取用户名。在使用表单身份验证而不是 Windows 身份验证时有什么方法可以做到这一点?

【问题讨论】:

  • 我认为将 <identity impersonate="true" /> 添加到 webconfig (就在 <authentication> 位下方,而不是内部!)应该可以满足您的需求。那么Controller应该有一个User可以查询的属性,而不是上面的代码……
  • 我刚刚试了一下,得到了 Principal.GenericIdentity

标签: asp.net .net asp.net-mvc iis-7 forms-authentication


【解决方案1】:

关闭 IIS 中的 Windows 身份验证,然后一切正常

【讨论】:

    【解决方案2】:

    请试试这个:

    在 web.config 中

    <identity impersonate="true" />
    <authentication mode="Forms">
         <forms loginUrl="./WebLogin.aspx" defaultUrl="./default.aspx" timeout="12" path="/" name=".ASPXFORMSAUTH" />
    </authentication>
    

    在代码c#中

    string windowsName = WindowsPrincipal.Current.Identity.Name;
    //OR
    string windowsName = Thread.CurrentPrincipal.Identity.Name;
    

    这对我来说很好。

    【讨论】:

    • 不就是获取IIS服务器运行的用户名吗?
    猜你喜欢
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 2013-10-30
    • 2016-02-02
    • 1970-01-01
    相关资源
    最近更新 更多