在混合模式身份验证中获取Windows userName

时间:2014-11-25 21:30:18

标签: asp.net authentication iis cookies

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

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

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中获取用户名。任何帮助将不胜感激,

非常感谢!

1 个答案:

答案 0 :(得分:0)

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

谢谢你。

相关问题