【问题标题】:getting windows userName in Mixed mode Authentication在混合模式身份验证中获取 Windows 用户名
【发布时间】:2015-01-24 01:37:09
【问题描述】:

我需要在现有的表单身份验证 Web 应用程序之上实现 Windows 身份验证,以获取我的应用程序中某些页面的 Windows 用户名。为此,我正在关注这篇文章 - Mixed Mode Authentication

如本文所述,我在 IIS 中的同一网站下创建了一个具有“Windows”身份验证的附加应用程序。我在 Application_AuthenticateRequest 中将 'userName' 设置为 cookie 票证的用户数据

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
      WindowsIdentity ident = WindowsIdentity.GetCurrent();
      WindowsPrincipal wind_princ = new WindowsPrincipal(ident); 
      string theUserName = wind_princ.Identity.Name.ToString();   
      FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, "qaAuto_winAuth_Ticket", DateTime.Now, DateTime.Now.AddMinutes(120), false, theUserName, "/");

       string encTicket = FormsAuthentication.Encrypt(ticket);
       Response.Cookies.Add(new HttpCookie("myTicket", encTicket));
       Response.Redirect("http://FormsAuthenticationApp/default.aspx");  
}

调试时,我可以看到 cookie 票证具有正确的用户名值,例如 - myDomain/myWindowsUserName。

但是在重定向之后,在 FormsAuthenticationApp 的“Application_AuthenticateRequest”中,当我读取此 cookie 的值时,我得到的是 NT AUTHORITY\NETWORK SERVICE,而不是在 windows 身份验证 myDomain/myWindowsUserName 中设置的内容。

过去两天一直在摸索如何在 FormsAuthenticationApp 中获取用户名。任何帮助将不胜感激,

非常感谢!

【问题讨论】:

    标签: asp.net authentication iis cookies


    【解决方案1】:

    我得到了这个工作 - 昨天当我发布问题时,我只在 IIS 下为我的 Win Auth 应用程序禁用了“匿名身份验证”(当然启用了“Windows 身份验证”),今天我尝试禁用所有“身份验证”模式,除了“Windows” - 即匿名、基本、摘要和表单,仅启用“Windows 身份验证”。另外,我更改了从 WindowsPrincipal 获取用户。由于我将模拟设置为 true,因此我更改为从 HttpContext.Current.User.Identity.Name 获取用户名。这成功了——现在我通过 cookie 将 Windows 用户名转移到我的表单身份验证站点。将此作为答案发布,因为这可能会对某人有所帮助。

    谢谢大家。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      • 2015-07-18
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      相关资源
      最近更新 更多