.Net 5 调用 HttpContext.SignInAsync 报错 Microsoft.AspNetCore.Authentication.AuthenticationService.Chall

Posted 键盘上落下的泪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.Net 5 调用 HttpContext.SignInAsync 报错 Microsoft.AspNetCore.Authentication.AuthenticationService.Chall相关的知识,希望对你有一定的参考价值。

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
    那就看我的
     
    在控制器中
    //获取到用户信息
                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

    文件中

     public void ConfigureServices(IServiceCollection services){
    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)

                       .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o =>

                       {

                           o.LoginPath = new PathString("/manage/home/Login");

                           o.AccessDeniedPath = new PathString("/Error/Forbidden");

                       });

    }
    
    
     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();
    }
    

     完成

     

以上是关于.Net 5 调用 HttpContext.SignInAsync 报错 Microsoft.AspNetCore.Authentication.AuthenticationService.Chall的主要内容,如果未能解决你的问题,请参考以下文章

c#调用R

调用 Abandon() 后 ASP.net MVC 5 会话未清除

如何在 ASP.Net MVC 5 中调用嵌套 ajax 调用并将对象数据发送到控制器

从 ASP.NET MVC 项目调用 .NET CORE 5 Web API 微服务时无法检索 BadRequest 错误

.Net 5 调用 HttpContext.SignInAsync 报错 Microsoft.AspNetCore.Authentication.AuthenticationService.Chall

使用 .NET Framework 3.5 在 C# 中调用 Web API