2016-04-09 15 views
2

Identityの代わりにCookie認証を使用するように、デフォルトのWebアプリケーションテンプレートを変更したいと考えています。だからここに私がやったことだ:私は制限されたリソース(/ホームにアクセスしようとしたときASP.NET Core MVC - IDなしのCookieミドルウェア

1 /何を削除するには、Identity

2 /このガイドに従ってくださいhttps://docs.asp.net/en/latest/security/authentication/cookie.html

問題

を含み、 /秘密)、私はログインページ=>に正しくリダイレ​​クトされました

私は、電子メール/パスワードを入力し、クライアント=>正しい行動で作成.AspNet.MyCookieMiddlewareInstanceという名前=>クッキーを提出します。

しかしが、その後、私はアカウントにリダイレクトされてしまった/代わりにシークレット/ホーム/のをアクセス拒否。 /アカウント/アクセスデベロッパーはどこから来たのですか?

私はそれを理解していないようでした。ここで私を助けてくれますか?

ありがとうございました

答えて

5

私は同じ問題がありました。いくつかの研究と調整の後、それは働いた... 今私は問題が次のようだと思う。最初に私は

var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(myclaims)); 

を以下のように構成された校長があったが、実際にそれが今、この文字列「MyCookieMiddlewareInstanceは」apperently設定する必要があります。この

var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(myclaims, "MyCookieMiddlewareInstance")); 

ようにされている必要があります。これもそのちょっと遅れている場合に役立ちますStartup.cs


public void Configure(IApplicationBuilder app) 
    { 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationScheme = "MyCookieMiddlewareInstance", 
      LoginPath = new PathString("/Auth/Login"), 
      AccessDeniedPath = new PathString("/Auth/Denied"), 
      AutomaticAuthenticate = true, 
      AutomaticChallenge = true 
     }); 


     app.UseMvc(routes => 
     { 
      routes.MapRoute(
       "default", 
       "{controller=Home}/{action=Index}/{id?}"); 
     }); 
    } 

そしてAuthController.csで

[HttpPost] 
    public ActionResult Login(LoginModel model) 
    { 
     if (model.Username == "test" && model.Password == "pass") 
     { 
      var myclaims = new List<Claim>(new Claim[] { new Claim("Id", "SOME USER ID FROM SOMEWHERE!!") }); 

      var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(myclaims, "MyCookieMiddlewareInstance")); 

      HttpContext.Authentication.SignInAsync("MyCookieMiddlewareInstance", claimsPrincipal).Wait(); 

      return RedirectToAction("Index", "Home"); 
     } 

     return View(new LoginModel()); 
    } 

希望:
また、これは完全なコンフィギュレーションとコントローラになります。

0

これは、新しいClaimsIdentityインスタンスを作成するために使用するコンストラクタが原因で発生します。認証タイプを指定しない場合、IsAuthenticatedプロパティーはfalseに設定され、記述したエラーが発生します。

Here is a blog post on this topic

特許請求の範囲を持っているClaimsIdentityを作成できるようになりましたが、falseに設定するにisAuthenticatedました。 ...

var id = new ClaimsIdentity(claims, “Custom”);

: がtrueに設定されているにisAuthenticatedするには、CTORで認証タイプを指定する必要があります
関連する問題