[英]How to add a cookie based authenticationScheme in ASP.NET Core?
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. 我想在控制器类中使用[Authorize]
属性将未登录的用户重定向到我的登录页面。 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: 但是当我使用[Authorize]
属性访问控制器时出现错误:
InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. InvalidOperationException:未指定authenticationScheme,也没有找到DefaultChallengeScheme。
I know I'm missing something, just not sure how to set up the authenticationScheme or use a default one. 我知道我缺少什么,只是不确定如何设置authenticationScheme或使用默认的。
You need to set the default AuthenticationScheme. 您需要设置默认的AuthenticationScheme。
AuthenticationScheme passed to AddAuthentication sets the default authentication scheme for the app. 传递给AddAuthentication的AuthenticationScheme设置应用程序的默认身份验证方案。
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");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.