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

标签 authentication asp.net-web-api asp.net-core-mvc restful-authentication

如果有人发现我的问题非常基本或者可能没有多大意义,我完全理解。 我对此很陌生,我正在尝试使用最新的 .NET Framework 5 和 MVC 6 来构建可以从 Angular JS 客户端使用的 Web Api。这将允许我为它创建一个网站,以及通过用 Phonegap 包装它来创建一个移动应用程序。所以请耐心等待。

我目前想要实现的是拥有一个Web API Controller ,它接收登录请求并基于Cookie身份验证将结果返回给客户端(稍后客户端应该存储此cookie并使用它与服务器)

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

enter image description here

  • 在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();
    
  • 在 Startup.cs 中的“配置”下:

        // Using the identity that technically should be calling the UseCookieAuthentication
        app.UseIdentity();
    

现在,在登录的 Controller 方法中,我可以使用其电子邮件地址和 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);

它抛出以下错误:

Error: No authentication handler is configured to handle the scheme: Microsoft.AspNet.Identity.Application

我目前被阻止,我在谷歌上搜索了几乎所有我能想到的可能的 token ,并尝试了多种解决方案,但仍然没有成功。非常感谢任何帮助。

问候,

最佳答案

好吧,在写完整个问题后,我终于弄清楚了,我想分享答案,以避免其他人犯与我相同的错误时的麻烦!

问题是在 Startup.cs 的配置中,我在调用“app.UseMVC()”后调用了“app.UseIdentity()”。顺序应该颠倒过来。我不知道这是否是常识,或者我应该在某处读过。

关于authentication - 使用 ASP.NET 5 MVC 6 Web API 进行 Cookie 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35779811/

相关文章:

c# - .Net Core 2.1.2 版本设置和 Controller 脚手架(序列不包含任何元素)

c# - 带有 Identity 2 和 EntityFramework 6 的 ASP.NET Core MVC(甲骨文)

.net - Google 联系人 API v3 身份验证

c# - 通用 Web Api 方法

asp.net - 手动将 MVC Web 应用从 3 升级到 4,无法添加 API Controller

asp.net-core - 具有自定义要求/处理程序的 .Net Core 授权

bower - Bower中的Visual Studio 2015还原程序包不执行任何操作

c# - 使用证书身份验证访问 Web 服务和 HTTP 接口(interface)

java - 如何在Spring Security中为某种类型的用户显示不同的页面

security - session 固定 - 登录后删除 session 并创建新 session - 但用户不再登录