2017-09-01 88 views
0

我想在應用程序ASP.NET Core 2中提供授權。 在帳戶/登錄中發送帶有數據的模型後,在調用「await驗證(用戶)「我收到一條錯誤消息。 我不明白哪裏缺乏說明。AS.NET Core 2未配置身份驗證處理程序以處理該方案

Startup.cs

 //ConfigureServices 
     services.AddAuthentication(options => 
     { 
      options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
      options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
      options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; 

     }).AddCookie("TmiginScheme", options => 
     { 
      options.LoginPath = "/Account/Login"; 
      options.LogoutPath = "/Account/Logout"; 
      options.ExpireTimeSpan = TimeSpan.FromHours(1); 
      options.SlidingExpiration = true; 
     }); 

     //Configure 
     app.UseAuthentication(); 

的AccountController

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public async Task<IActionResult> Login(LoginModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      User user = null; 
      Cryptex cryptex = new Cryptex(); 
      string password = cryptex.EncryptText(model.Password, "TMigin"); 

      // Ищем user 
      user = fStorage.Users.GetUserByLogin(model.Login); 
      if (user != null) 
      { 
       if (string.Compare(user.Password, password) != 0) 
       { 
        user = null; 
       } 
      } 

      if (user != null) 
      { 
       await Authenticate(user); 

       return RedirectToAction("Index", "CMS"); 
      } 
      else 
      { 
       // Логируем ошибку входа 
       ModelState.AddModelError("", "Ошибка входа"); 
      } 
     } 

     return View(model); 
    } 

    private async Task Authenticate(User user) 
    { 
     var claims = new List<Claim> 
       { 
        new Claim(ClaimsIdentity.DefaultNameClaimType, user.Name), 
        new Claim("CMS", "True") 
       }; 
     var identity = new ClaimsIdentity(claims); 
     var principal = new ClaimsPrincipal(identity); 
     await HttpContext.Authentication.SignInAsync("TmiginScheme", principal); 
    } 

固定

不工作,因爲我把app.UseMvc後的代碼(... ){}。 在屏幕截圖中顯示正確的位置。 correctly

回答

1

我想這個問題是要配置的默認方案是Cookies,當你使用options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;,比你用不同的計劃,該計劃是TmiginScheme當你使用AddCookie("TmiginScheme"

AccountController相比,您在未指定驗證類型的情況下創建了新的ClaimsIdentity,最後嘗試使用與您在options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;中指定的方案名稱不同的方案名稱登錄。

要解決您的問題更改AddCookie("TmiginScheme".AddCookie(CookieAuthenticationDefaults.AuthenticationScheme

變化var identity = new ClaimsIdentity(claims);變爲var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

最後改變await HttpContext.Authentication.SignInAsync("TmiginScheme", principal);await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

3

我有同樣的問題,但筆者的解決方案並沒有爲我工作。 我從.NET Core 1.1遷移到.NET Core 2.0。

在我來說,我用的是:

await HttpContext.Authentication.SignInAsync(...); 
await HttpContext.Authentication.SignOutAsync(...); 

,我應該使用:

await HttpContext.SignInAsync(...); 
await HttpContext.SignOutAsync(...); 
+0

你是一個救星。我一直在嘗試固體8小時以上,結果證明這是修復。 –

相關問題