[英]How do I authenticate user in ASP.NET Identity framework?
I am using a default ASP.NET 4.5 framework's ASP.NET MVC 5 project template. 我正在使用默认的ASP.NET 4.5框架的ASP.NET MVC 5项目模板。 It has identity authentication framework configured with it.
它具有配置的身份验证框架。
I want to use Window's Active Directory with my project, so I followed this article - http://www.schiffhauer.com/mvc-5-and-active-directory-authentication/ 我想在我的项目中使用Window的Active Directory,所以我遵循了这篇文章-http: //www.schiffhauer.com/mvc-5-and-active-directory-authentication/
And in my AccountController's login method I have 在我的AccountController的登录方法中,
if (Membership.ValidateUser(model.Email, model.Password))
{
CustomFormsAuthentication.SetAuthenticationCookie(model.Email, model, model.RememberMe);
return RedirectToAction("Index", "Home");
}
Where CustomFormsAuthentication class has 其中CustomFormsAuthentication类具有
public static class CustomFormsAuthentication
{
public static void SetAuthenticationCookie(string username, object obj, bool isPersistent)
{
const int version = 1;
var json = new JavaScriptSerializer().Serialize(obj);
var cookieStoreTime = isPersistent ? DateTime.Now.AddDays(7): DateTime.Now.AddDays(1);
var ticket = new FormsAuthenticationTicket(version, username, DateTime.Now, cookieStoreTime, isPersistent, json);
HttpContext.Current.Response.Cookies.Set(new HttpCookie(FormsAuthentication.FormsCookieName,
FormsAuthentication.Encrypt(ticket)) {Expires = cookieStoreTime});
}
}
But the user is never authenticated. 但是用户永远不会被认证。 The User.Identity.IsAuthenticated still shows false.
User.Identity.IsAuthenticated仍显示为false。
How do I go about this ? 我该怎么办?
Identity is incompatible with forms authentication (utilized by ASP.NET Membership here in your code). 身份与表单身份验证不兼容(由代码中的ASP.NET成员资格使用)。 Even then, forms authentication is incompatible with Windows auth, which also is incompatible with Identity.
即使这样,表单身份验证也与Windows身份验证不兼容,Windows身份验证也与Identity不兼容。 If you want to authenticate using Windows credentials, then you need to change your Web.config to have:
如果要使用Windows凭据进行身份验证,则需要将Web.config更改为具有:
...
<system.web>
...
<authentication mode="Windows"/>
...
</system.web>
...
And then you can pretty much just remove all the Identity stuff as it won't work any more. 然后您几乎可以删除所有Identity内容,因为它将不再起作用。
EDIT 编辑
The most important point here after the discussion in the comments is that multiple forms of authentication simultaneously is not a supported scenario in ASP.NET. 在注释中进行讨论之后,此处最重要的一点是,ASP.NET不支持同时进行多种形式的身份验证。 You can technically do workarounds such that you basically proxy authentication to an LDAP server to verify AD credentials and then create/get an Identity/Membership user based on that to actually be the authenticated user.
从技术上讲,您可以执行一些变通办法,以便基本上将身份验证代理到LDAP服务器,以验证AD凭据,然后基于该身份创建/获取一个身份/会员身份用户,使其成为经过身份验证的用户。
However, here, I don't see any real attempt to do that, and even then, you're using ASP.NET Membership and forms auth to handle it, which precludes the use of Identity. 但是,在这里,我没有看到任何真正的尝试,即使如此,您仍在使用ASP.NET成员资格和表单auth来处理它,从而无法使用Identity。 Identity and forms auth are completely incompatible with each other.
身份和表单auth完全不兼容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.