简体   繁体   English

如何在ASP.NET Identity框架中对用户进行身份验证?

[英]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.

相关问题 我如何使用 ASP.NET Core 3.1 身份验证和注册来自业务逻辑层的用户 - How do i Authenticate and register users from the business Logic Layer with ASP.NET Core 3.1 Identity 如何检查用户是否在 Asp.Net Core Identity Framework 中担任角色 - How to check if a user is in a role in Asp.Net Core Identity Framework 如何在 Identity ASP.NET Core 2.1(Identity Scaffolded)中播种用户角色 - How do I seed user roles in Identity ASP.NET Core 2.1 (Identity Scaffolded) 如何使用AspNet.Identity使用Asp.Net MVC5 RTM位登录/验证用户? - How do you login/authenticate a user with Asp.Net MVC5 RTM bits using AspNet.Identity? ASP.NET身份框架(表单)如何使用bootstrap - How do ASP.NET Identity framework (forms) with bootstrap 成功登录后,ASP.NET Identity无法验证用户身份 - ASP.NET Identity does not authenticate user after successful sign in 如何使用ASP.NET Identity 2.0允许用户模拟其他用户? - How do I use ASP.NET Identity 2.0 to allow a user to impersonate another user? 一旦用户通过Asp.Net中的成员资格和角色创建新帐户,我该如何进行身份验证 - How do i authenticate once the user create a new Account by Membership & Roles in Asp.Net 我如何验证第三方用户访问我的Web API asp.net? - how do i authenticate 3rd party user to access my web API asp.net? 用户身份框架asp.net身份和LDAP - User Identity Framework asp.net Identity and LDAP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM