.Net 5 调用 HttpContext.SignInAsync 报错 Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, string scheme, AuthenticationProperties properties) 解决
An unhandled exception occurred while processing the request.
InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).
Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, string scheme, AuthenticationProperties properties)
- Stack
- Query
- Headers
- Routing
-
InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).
-
Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, string scheme, AuthenticationProperties properties)
-
Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
-
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
-
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
-
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
-
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
-
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
-
官网解决之道,不过意思没太看懂https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/cookie?view=aspnetcore-5.0#configuration那就看我的在控制器中123456789101112131415161718192021222324252627282930313233343536//获取到用户信息
var
user = await _userService.UserLogin(vm);
var
claims =
new
List<Claim>
{
new
Claim(ClaimTypes.NameIdentifier, user.UserId),
new
Claim(ClaimTypes.Name, user.UserName),
new
Claim(
"UserDataInfo"
, user.ToJson()),
new
Claim(ClaimTypes.Role,
"Administrator"
),
};
var
claimsIdentity =
new
ClaimsIdentity(claims, Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme);
var
authProperties =
new
AuthenticationProperties
{
//应该允许刷新身份验证会话。
AllowRefresh =
false
,
//认证票据过期的时间。
// 一个value将覆盖ExpireTimeSpan选项
//CookieAuthenticationOptions设置AddCookie。
ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10),
//身份验证会话是否持久化
// 多个请求。当与cookie、控件一起使用时
//是否cookie的生存期是绝对的(匹配
//认证票据的生命周期)或基于会话的。
IsPersistent =
false
,
//颁发身份验证票据的时间。
IssuedUtc = DateTimeOffset.UtcNow,
//作为http的完整路径或绝对URI
//重定向响应值。
RedirectUri =
"/Admin/User/Login"
};
await HttpContext.SignInAsync(Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme,
new
ClaimsPrincipal(claimsIdentity), authProperties);
在Startup.cs
文件中
123456789101112public
void
ConfigureServices(IServiceCollection services){
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)}
public
void
Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//注意app.UseAuthentication方法一定要放在下面的app.UseMvc方法前面,否者后面就算调用HttpContext.SignInAsync进行用户登录后,使用
//HttpContext.User还是会显示用户没有登录,并且HttpContext.User.Claims读取不到登录用户的任何信息。
//这说明Asp.Net OWIN框架中MiddleWare的调用顺序会对系统功能产生很大的影响,各个MiddleWare的调用顺序一定不能反
app.UseAuthentication();
app.UseAuthorization();
}
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o =>
{
o.LoginPath = new PathString("/manage/home/Login");
o.AccessDeniedPath = new PathString("/Error/Forbidden");
});
完成
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现