我的MVC5.1应用程序使用基于http://www.khalidabuhakmeh.com/asp-net-mvc-5-authentication-breakdown的OWIN身份验证
db和login验证中的用户持久性由我的自定义UserService类处理 . 当使用个人帐户登录系统时,这非常适用 .
我还需要使用Windows身份验证(Active Directory) . 因此,如果打开了Windows身份验证(在IIS上并且在我的web.config中有一个标志),则将对我的数据库验证AD用户,以查看登录用户是否可以访问我的Web应用程序,如果是,则登录他(设置应用程序cookie) .
我在IIS中启用了Windows Auth,在IIS中禁用了匿名身份验证,并从我的web.config文件中删除了 <authentication mode="None" />
. 这使得OWIN认为用户已经过身份验证,即使在启动时我将其配置为使用ApplicationCookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
好吧,这很好,我可以编写一个围绕AuthenticationManager.User.Identity.IsAuthenticated属性的包装器,并添加检查以验证用户对我的数据库 . 但后来我无法添加自定义声明,因为OWIN似乎没有使用应用程序Cookie但直接使用WindowsClaimsIdentity .
我的登录功能(尝试添加其他声明的功能)如下:
_authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
var identity = new ClaimsIdentity(
new[] {
new Claim(ClaimTypes.NameIdentifier, user.UserID.ToString()),
new Claim(ClaimTypes.Name, user.UserName),
new Claim(ClaimTypes.UserData, user.CurrentDomainID.ToString())
},
DefaultAuthenticationTypes.ApplicationCookie,
ClaimTypes.Name,
ClaimTypes.Role);
_authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
现在,如果我在web.config中添加行 <authentication mode="None" />
,它似乎正在工作,这意味着OWIN只在其cookie中查找声明,并且在用户存在于数据库中之前不会对用户进行身份验证 .
But in both these cases, I am stuck in a redirect loop on the login page unless i remove login path from the startup. I tried setting Response.SuppressFormsAuthenticationRedirect=false in my login controller but no change.
Another problem I am facing is, the browser doesnt show me the login page until I enter a valid username/password in the basic auth popup even if i am logged in with my AD account in windows. Is there a way to hide this popup?