2

我完全理解是否有人发现我的问题非常基本,或者可能不会一直有很大意义。 我对此很陌生,我试图使用最新的.NET Framework 5与MVC 6,以建立一个可以从Angular JS客户端使用的Web Api。这将允许我为它创建一个网站,以及通过用Phonegap包装它的移动应用程序。所以请耐心等待。使用ASP.NET 5 MVC 6 Web API进行Cookie身份验证

我现在想实现的目标是拥有一个Web API控制器,它接收一个登录请求并根据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(); 
    

,在控制器的方法来登录,我能够使用其电子邮件地址和的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(); 

问题是一个发生t为线:

  // 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

我目前受阻,我搜索用Google搜索,几乎每一个可能的令牌我能想到的和没有白费仍然尝试多种解决方案。任何帮助,高度赞赏。

问候,

回答

5

好吧,我终于想通了写这整个问题后,我想分享的答案,以避免别人的拼劲,如果他们犯我做了同样的错误!

问题是在调用“app.UseMVC()”后,在Startup.cs中的Configure中,我调用了“app.UseIdentity()”。该命令应该已被反转。我不知道,如果这是常识,或者我应该在某处阅读它。

+0

这就像一个“假设”知道处理类似owin管道的未规则规则。大多数这些东西已经被遮盖了很长时间,现在由于管道模型,它们是显而易见的。 –