其他分享
首页 > 其他分享> > 重定向登录时返回401

重定向登录时返回401

作者:互联网

context.Services.ConfigureApplicationCookie(options =>
    options.Events.OnRedirectToLogin = httpContext =>
    {
        httpContext.Response.ContentType = "application/json;charset=utf-8";
        httpContext.Response.Headers["Location"] = httpContext.RedirectUri;
        httpContext.Response.StatusCode = StatusCodes.Status401Unauthorized;

        using (var scope = context.Services.BuildServiceProvider())
        {
            var result = new RemoteServiceErrorResponse(
                new RemoteServiceErrorInfo
                {
                    Code = httpContext.Response.StatusCode.ToString(),
                    Message = "Not certified."
                });

            httpContext.Response.WriteAsync(scope.GetRequiredService<IJsonSerializer>().Serialize(result));
        }
        return System.Threading.Tasks.Task.CompletedTask;
    });

 

标签:重定向,登录,Services,401,var,scope,httpContext,Response,StatusCode
来源: https://www.cnblogs.com/xinzheng/p/16611843.html