简体   繁体   English

如何在ASP.NET Core中添加基于cookie的authenticationScheme?

[英]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。

As the docs says 正如文档所说

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.

相关问题 如何在asp.net core中为cookie添加前缀? - How to add prefix to cookie in asp.net core? 没有指定authenticationScheme,也没有找到DefaultChallengeScheme-ASP.NET Core 2.1 - No authenticationScheme was specified, and there was no DefaultChallengeScheme found - ASP.NET core 2.1 ASP.NET MVC 将 RememberMe 复选框添加到 @model AuthenticationScheme[] - ASP.NET MVC add RememberMe checkbox to @model AuthenticationScheme[] 如何在ASP.net Core中基于环境添加DbContext - How to add DbContext based on environment in ASP.net Core 多个“本地主机”Asp.Net Core 站点上基于 Cookie 的身份验证 - Cookie based authentication on multiple 'localhost' Asp.Net Core Sites 使用 ASP.NET 核心添加一个新的持久性 cookie - Add a new persistent cookie using ASP.NET core ASP.NET Core 3.1 Web API 中的“授权失败。AuthenticationScheme:AzureADJwtBearer 受到挑战” - 401 Unauthorized - "Authorization failed. AuthenticationScheme: AzureADJwtBearer was challenged" in ASP.NET Core 3.1 Web API - 401 Unauthorized ASP.NET Core 2.0 HttpSys Windows 身份验证因 Authorize 属性而失败(InvalidOperationException:未指定身份验证方案) - ASP.NET Core 2.0 HttpSys Windows Authentication fails with Authorize attribute (InvalidOperationException: No authenticationScheme was specified) 共享 cookie .net Core 3 和 Asp.net - Share cookie .net Core 3 and Asp.net 如何在 ASP.NET Core 中设置 cookie validateInterval? - How to set the cookie validateInterval in ASP.NET Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM