【问题标题】:Cookie Authentication with ASP.NET 5 MVC 6 Web API使用 ASP.NET 5 MVC 6 Web API 进行 Cookie 身份验证
【发布时间】:2016-06-17 05:30:44
【问题描述】:

如果有人发现我的问题非常基本或者可能一直没有多大意义,我完全理解。 我是新手,我正在尝试将最新的 .NET Framework 5 与 MVC 6 一起使用,以构建可以从 Angular JS 客户端使用的 Web Api。这将允许我为它创建一个网站,以及通过使用 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();
    
  • 在 Startup.cs 中的配置下:

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

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

我目前被阻止,我搜索了几乎所有我能想到的可能的令牌,并尝试了多种解决方案,但仍然没有白费。非常感谢任何帮助。

问候,

【问题讨论】:

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


    【解决方案1】:

    好的,在写完整个问题后,我终于想通了,我想分享答案,以免其他人犯我犯的同样错误!

    问题是在 Startup.cs 的配置中,我在调用“app.UseMVC()”之后调用了“app.UseIdentity()”。顺序应该是颠倒的。我不知道这是常识还是我应该在某个地方读到它。

    【讨论】:

    • 这就像一个未说的规则,在处理类似 owin 的管道时“应该”知道。大多数这些东西已经被掩盖了很长时间,现在由于管道模型,它们变得很明显。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 2016-05-25
    • 2015-09-17
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 2015-01-12
    相关资源
    最近更新 更多