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

     完成

     
posted @ 2021-05-19 13:45  LuoCore  阅读(1068)  评论(0编辑  收藏  举报