2017-09-01 5 views
0

アプリケーションのASP.NETコア2に認証を提供します。 アカウント/ログインのデータを含むモデルを送信した後、 "await Authenticate(user) "エラーメッセージが表示されます。 説明が不足している箇所はわかりません。AS.NETコア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

私は、問題は、あなたがoptions.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;、あなたがAddCookie("TmiginScheme"を使用TmiginSchemeある別のスキームを使用するよりも、使用時にデフォルトのスキームはCookiesする設定されていると思います。

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);

3

await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);に変更します。 私は.NET Core 1.1から.NET Core 2.0に移行していました。

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

をし、私が使用する必要があります:私の場合は

私が使っていたあなたは命の恩人です

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

。私は固体の8時間以上のものを試してきましたが、これは修正されたことが判明しました。 –

関連する問題