Authentication 使用ASP.NET 5 MVC 6 Web API进行Cookie身份验证

Authentication 使用ASP.NET 5 MVC 6 Web API进行Cookie身份验证,authentication,asp.net-web-api,asp.net-core-mvc,restful-authentication,Authentication,Asp.net Web Api,Asp.net Core Mvc,Restful Authentication,我完全理解如果有人发现我的问题很基本,或者一路上可能没有什么意义。 我是新手,我正在尝试使用最新的.NETFramework5和MVC6来构建一个可以从JS客户端使用的WebAPI。这将允许我创建一个网站,以及一个移动应用程序包装它与Phonegap。所以请容忍我一点 目前我试图实现的是一个Web API控制器,它接收登录请求并根据Cookie身份验证将结果返回给客户端(稍后客户端应存储此Cookie并使用它与服务器通信) 我在project.json中添加了以下内容 在Startup.

我完全理解如果有人发现我的问题很基本,或者一路上可能没有什么意义。 我是新手,我正在尝试使用最新的.NETFramework5和MVC6来构建一个可以从JS客户端使用的WebAPI。这将允许我创建一个网站,以及一个移动应用程序包装它与Phonegap。所以请容忍我一点

目前我试图实现的是一个Web API控制器,它接收登录请求并根据Cookie身份验证将结果返回给客户端(稍后客户端应存储此Cookie并使用它与服务器通信)

  • 我在project.json中添加了以下内容

  • 在Startup.cs中,我在ConfigureServices下添加了:

    // Add entity framework support
    services.AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<ApplicationDbContext>(options =>
        {
            options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]);
        });
    
        // add ASP.NET Identity
        services.AddIdentity<ApplicationUser, IdentityRole>(options => {
            options.Password.RequireDigit = false;
            options.Password.RequireLowercase = false;
            options.Password.RequireUppercase = false;
            options.Password.RequireNonLetterOrDigit = false;
            options.Password.RequiredLength = 6;
        })
           .AddEntityFrameworkStores<ApplicationDbContext>()
           .AddDefaultTokenProviders();
    
现在,在控制器登录方法中,我可以使用其电子邮件地址和UserManager找到用户:

            // Verify that the model is valid according to the validation rules in the model itself. 
            // If it isn't valid, return a 400 Bad Request with some JSON reviewing the errors
            if (!ModelState.IsValid)
            {
                return HttpBadRequest(ModelState);
            }

            // Find the user in our database.  If the user does not exist, then return a 400 Bad Request with a general error.
            var user = await userManager.FindByEmailAsync(model.Email);
            if (user == null)
            {
                ModelState.AddModelError("", INVALID_LOGIN_MESSAGE);
                return HttpBadRequest(ModelState);
            }

            // If the user has not confirmed his/her email address, then return a 400 Bad Request with a request to activate the account.
            if (!user.EmailConfirmed)
            {
                ModelState.AddModelError("Email", "Account not activated");
                return HttpBadRequest(ModelState);
            }

            // Authenticate the user with the Sign-In Manager
            var result = await signInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, lockoutOnFailure: false);
            // If the authentication failed, add the same error that we add when we can't find the user
            // (so you can't tell the difference between a bad username and a bad password) and return a 400 Bad Request
            if (!result.Succeeded)
            {
                ModelState.AddModelError("", INVALID_LOGIN_MESSAGE);
                return new BadRequestObjectResult(ModelState);
            }

            return Ok();
问题发生在生产线上:

            // Authenticate the user with the Sign-In Manager
            var result = await signInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, lockoutOnFailure: false);
它将抛出以下错误:

错误:未配置任何身份验证处理程序来处理方案: Microsoft.AspNet.Identity.Application

我现在被屏蔽了,我在谷歌上搜索了我能想到的几乎所有可能的令牌,并尝试了多种解决方案,但仍然没有白费。非常感谢您的帮助


问候,

好的,在写了整个问题后,我终于明白了,我想分享答案,以避免其他人犯下与我相同的错误


问题是,在Startup.cs中的Configure中,我在调用“app.UseMVC()”之后调用了“app.UseIdentity()”。顺序应该颠倒过来。我不知道这是否是常识,或者我应该在什么地方读到它

这就像一条未说的规则,在处理类似owin的管道时,“应该”知道这条规则。这些东西中的大多数已经被掩盖了很长一段时间,现在由于管道模型,它们是显而易见的。
            // Authenticate the user with the Sign-In Manager
            var result = await signInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, lockoutOnFailure: false);