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管道的未規則規則。大多數這些東西已經被遮蓋了很長時間,現在由於管道模型,它們是顯而易見的。 –