C# Windows身份验证问题
我正在尝试设置C# Windows身份验证问题,c#,session,authentication,forms-authentication,C#,Session,Authentication,Forms Authentication,我正在尝试设置表单身份验证使用角色登录和注销。为什么设置了这些配置后,@User.Identity.Name返回Windows域用户名 <authentication mode="Forms"> <forms name="Login" loginUrl="~/User/Login" defaultUrl="~/Home/Index" protection="All" timeout="90" slidingExpiration="true"/> </authen
表单身份验证
使用角色登录和注销。为什么设置了这些配置后,@User.Identity.Name
返回Windows域用户名
<authentication mode="Forms">
<forms name="Login" loginUrl="~/User/Login" defaultUrl="~/Home/Index" protection="All" timeout="90" slidingExpiration="true"/>
</authentication>
在注销期间,我将同一个cookie设置为空,并将会话.放弃()
这是我显示用户名的方式:
@if (User != null)
{
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">
Logged in as @User.Identity.Name
<span class="caret"></span>
</a>
@if(用户!=null)
{
以@User.Identity.Name登录
我已经尝试了各种各样的方法,但我在这里拔出了我的头发,无法理解我哪里出了问题。你做得不正确,当然,是你创建了两个auth cookies 第一个来自
Response.Cookies.Add(cookie);
还有一个来自
FormsAuthentication.RedirectFromLoginPage(user.UserName, user.RememberMe);
注销后,您会有类似的副本。此行
FormsAuthentication.SignOut();
将过期的表单cookie追加到响应中,但您需要再次执行此操作
Response.Cookies.Add(cookie);
最后但并非最不重要的一点是,如果显示用户名,请确保该用户已自动验证
@if (User != null && User.Identity != null && User.Identity.IsAuthenticated)
否则,您可能会为未经验证的请求显示用户名
你应该做什么:
Authorize
@YvetteColomb添加到问题中。我认为cookie没有被清除或覆盖,因为web.config有一个将页面限制为管理员角色的设置。它没有被激发,因为用户总是登录。我希望它使用来宾状态(当我注销时,它会转到Windows Auth并显示域名)或者
user.UserName
而不是Windows Auth。希望这能更好地解释它。@WiktorZychla可能存在这种情况,但即使我从登录中删除Add,并从注销中注销,它仍然会持续显示Windows域用户名。浏览器cookie/缓存已被清除。Fiddler告诉我,登录时它会设置为这个奇怪的值lueLogin=c4befc0c83c0b077c8d8fd300f86…
等。注销时,cookie设置为此。set cookie:Login=;expires=Mon,22-Aug-2016 10:19:09 GMT;path=/
。似乎没有任何重复项。当框未被勾选时,它似乎还显示PersistentAuth=true
。@kamil1995b:thePersistenAuth
标题清楚地表明Windows身份验证仍然出于某种原因使用。换句话说,尽管Forms
是配置的提供程序,但仍然使用Windows
。遗憾的是,原因无法从您提供的事实中得出,我们只能推测。您是否修改了正确的网站.config
(根目录,而不是视图
文件夹下的目录)?如果是,你能在一个最简单的应用程序上重现这种错误行为并共享完整的代码以便进行检查吗?@YvetteColomb:OP仍在使用Kerberos对其应用程序进行身份验证,他们收到的PersistentAuth
头是一个明确的指示。看起来像是一个简单的配置错误,与他们使用的代码无关然而,埃森特。
Response.Cookies.Add(cookie);
@if (User != null && User.Identity != null && User.Identity.IsAuthenticated)