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()
で
はhttps://docs.microsoft.com/en-us/aspnet/core/security/authentication/ドキュメントで述べたように、あなたが 'services.ConfigureApplicationCookie'が欠落しているのだろうidentity?tabs = visual-studio%2Caspnetcore2x – Nkosi
あなたはASP.NET IDを使用しているのですか?現在、あなたのコードは両方を使用しています。 – Win