重定向登录时返回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;
    });

 

posted @ 2022-08-22 09:54  邪不压正!  阅读(159)  评论(0编辑  收藏  举报