2017-11-29 7 views
1

ASP.Net Core 2 MVCアプリケーションで認証が失われました。 私はCore 2のバージョンで作業しています。バージョン1と2の間に多くの変更があるようです。実際には動作しないチュートリアルを読んでいます。ASP.Net Core 2認証

まず第一に、ここで私はConfigureServices()方法でStartup.csに入れるものです:

services.AddIdentity<MyUserClass, IdentityRole>() 
       .AddEntityFrameworkStores<MyDatabaseEFContext>() 
       .AddDefaultTokenProviders(); 

services.ConfigureApplicationCookie(options => 
      { 
       // Cookie settings 
       options.Cookie.HttpOnly = true; 
       options.Cookie.Expiration = TimeSpan.FromDays(150); 
       options.LoginPath = "/Account/Login"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login 
       options.LogoutPath = "/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout 
       options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied 
       options.SlidingExpiration = true; 
      }); 

、ここでは、私がConfigure()方法に入れるものです:

app.UseIdentity(); 

私はこれを置きます各コントローラの各動作方法に関する注釈:

[Authorize] 

そして、ここでは、私は私のポストにアクションログイン方法何をやったかである:

[HttpPost] 
[AllowAnonymous] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> Index(LoginViewModel model, string returnUrl) 
{ 
    if (!ModelState.IsValid) 
    { 
     return View(model); 
    } 

    var claims = new List<Claim> {new Claim(ClaimTypes.Name, model.Login)}; 
    var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); 
    var principal = new ClaimsPrincipal(identity); 

    await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal); 

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

を私がログインしようとしたとき、私はこの例外を取得:

例外InvalidOperationException:認証なしハンドラが処理するように設定されていませんスキーム:クッキー

何が悪いと思いますか?ごConfigure()方法変更app.UseIdentity()

+0

はhttps://docs.microsoft.com/en-us/aspnet/core/security/authentication/ドキュメントで述べたように、あなたが 'services.ConfigureApplicationCookie'が欠落しているのだろうidentity?tabs = visual-studio%2Caspnetcore2x – Nkosi

+1

あなたはASP.NET IDを使用しているのですか?現在、あなたのコードは両方を使用しています。 – Win

答えて

2

:また

app.UseAuthentication(); 

は、注意:(それはあなたのIndexアクションで表示される)は、Identityせずにクッキーを使用している場合:

が起動しますAddAuthenticationおよびAddCookieの方法: ConfigureServices方法:

// If you don't want the cookie to be automatically authenticated and assigned to HttpContext.User, 
// remove the CookieAuthenticationDefaults.AuthenticationScheme parameter passed to AddAuthentication. 
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) 
     .AddCookie(options => 
     { 
      options.LoginPath = "/Account/LogIn"; 
      options.LogoutPath = "/Account/LogOff"; 
     }); 

追加読書:Migrating Authentication and Identity to ASP.NET Core 2.0

+0

a同じエラーが発生する... – Bob5421

+1

@ Bob5421:アイデンティティなしでクッキーを使用している場合は、ConfigureServicesに 'services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)'を追加する必要があります。更新された回答と[ASP.NET Core 2.0への認証とIDの移行](https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x) – CalC

+0

私はクッキーとアイデンティティの違いを理解していません。私が欲しいのは、PHPのようなセッションでの認証です:) – Bob5421

関連する問題