here の手順に従って、Cookie認証をサイトに追加しようとしています。
これまでのところ、私は以下を追加しました:
Startup.csファイルのConfigureメソッドでUseAuthenticationメソッドを呼び出します。
app.UseAuthentication();
Startup.csファイルのConfigureServicesメソッドでAddAuthenticationメソッドとAddCookieメソッドを呼び出します。
services.AddAuthentication("MyCookieAuthenticationScheme")
.AddCookie(options => {
options.AccessDeniedPath = "/Account/Forbidden/";
options.LoginPath = "/Account/Unauthorized/";
});
私のログインコードには次に
await HttpContext.SignInAsync("MyCookieAuthenticationScheme", principal);
principle
はClaimsPrincipal
です。
私のサイトにログインして上の行を呼び出すと、エラーが発生します。
InvalidOperationException:スキームのサインインを処理するように構成されたIAuthenticationSignInHandlerはありません:MyCookieAuthenticationScheme
何を逃したのですか?
デフォルトのスキームを "MyCookieAuthenticationScheme"(これはAddAuthentication
の最初の引数です)にしたいと言っていましたが、その名前の認証ハンドラーを追加していませんでした。 AddCookies
を呼び出したときに、スキーム「Cookies」を使用してハンドラーを追加しました(これがデフォルトです)。
コードを次のように変更する必要があります。
services.AddAuthentication("MyCookieAuthenticationScheme")
.AddCookie("MyCookieAuthenticationScheme", options =>
{
options.AccessDeniedPath = "/Account/Forbidden/";
options.LoginPath = "/Account/Unauthorized/";
});
プリミティブをよりよく理解するには、この記事を参照してください。
https://digitalmccullough.com/posts/aspnetcore-auth-system-demystified.html
呼び出し場所を変更してみてください_services.AddAuthentication(
_これは役に立ちました。
_public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
var builder = new ContainerBuilder();
builder.RegisterModule(new HowResolveDependencies());
services.AddTransient<IExceptionHandler, ExceptionToResponseWriter>();
builder.Populate(services);
services.AddAuthentication(AuthConsts.MainAuthScheme)
.AddCookie(
...
}
_
私のプロジェクトでは、builder.Populate(services)
の前に_services.AddAuthentication
_を配置することで問題が解決しました。