.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)

  • 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
    那就看我的
     
    在控制器中
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    //获取到用户信息
                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

    文件中

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public 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");

                       });

     完成

     
posted @   LuoCore  阅读(1092)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示