在未授权用户的自定义身份验证中显示 AuthenticateResult.Fail("Invalid Token Used for Authorisation" )

问题描述

这是自定义身份验证类,但它不会打印错误令牌密钥的消息(令牌密钥是每个用户的唯一字符串) =>AuthenticateResult.Fail("Invalid Token Used for Authorisation") 它只是在邮递员中显示未经授权的 401 状态代码
var valtoken = auth.getTokenDetails(token);如果具有指定令牌的用户有权访问控制器的 get 方法,则方法返回用户详细信息,否则返回 null。因此,对于空结果,我想返回未经授权的 401 状态代码以及 Get api 调用自定义消息

navigator.geolocation.getCurrentPosition(successCallback,errorCallback,{timeout:10000});

解决方法

我只是在 CustAuthHandler 中覆盖了 HandleChallengeAsync 方法,当授权失败时,这个方法被称为 Eveytime,所以我已经写回了一个字符串和 401 状态代码到我的 http 请求的响应主体,该请求的身份验证失败。

protected override async Task HandleChallengeAsync(AuthenticationProperties properties)
        {
            
            await base.HandleChallengeAsync(properties);
            Response.StatusCode = (int)HttpStatusCode.Unauthorized;
            string output = "authentification failed you don't have access to this content";
            await Response.BodyWriter.WriteAsync(Encoding.UTF8.GetBytes(output));
        
           
        }