I'd like to use the [Authorize]
attribute in my controller classes to redirect users who are not signed in, to my sign in page. For authentication, I'd like to keep it simple and just use a session variable to track if someone is signed in or not.
I tried adding authentication to my startup class:
services.AddAuthentication()
.AddCookie(options =>
{
options.AccessDeniedPath = new PathString("/Account/SignIn");
options.LoginPath = new PathString("/Account/SignIn");
options.LogoutPath = new PathString("/Home/SignOut");
});
but am getting the error when I go to a controller with the [Authorize]
attribute:
InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found.
I know I'm missing something, just not sure how to set up the authenticationScheme or use a default one.
You need to set the default AuthenticationScheme.
As the docs says
AuthenticationScheme passed to AddAuthentication sets the default authentication scheme for the app.
In your case
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.AccessDeniedPath = new PathString("/Account/SignIn");
options.LoginPath = new PathString("/Account/SignIn");
options.LogoutPath = new PathString("/Home/SignOut");
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.