【问题标题】:Return custom error message for grant_type field missing ASP.NET Web API返回缺少 ASP.NET Web API 的 grant_type 字段的自定义错误消息
【发布时间】:2021-10-19 16:34:55
【问题描述】:

我需要构建一个 API,它在没有 grant_type 字段或 scope 字段的 OAuth 请求上返回自定义错误消息。还有其他要求,我需要返回通过 grant_type 但不正确的自定义错误消息。我可以使用下面的代码来做到这一点

public override async Task ValidateTokenRequest(OAuthValidateTokenRequestContext context)
{      
            if (!context.TokenRequest.IsClientCredentialsGrantType)
            {
                context.SetTokenRequestError("invalid_grant", "Invalid grant type", "invalid_grant", "Grant type is invalid");
                return;
            }
            await Task.Run(() => context.Validated());
 }

public static void SetTokenRequestError(this OAuthValidateTokenRequestContext context, string code, string cmessage, string field, string fmessage)
        {
            var response = new
            {
                code = $"{code}",
                message = $"{cmessage}",
                errors = new[] {
                    new { field = $"{field}", message = $"{fmessage}"}
                }
            };

           
            var json = response.ToJsonString();
            context.SetError(json);
            context.Response.Write(json);
            InvokeTokenRequest(context);
        }

static async Task InvokeTokenRequest(OAuthValidateTokenRequestContext context)
{
            var owinResponseStream = new MemoryStream();
            context.Response.ContentType = "application/json";            
            context.Response.Body = owinResponseStream;
}

对以下令牌端点的 POST 请求在请求正文中没有 grant_type 字段应返回以下内容。对于我的生活,我找不到哪个 OAuth 中间件功能,我需要重写才能实现这一点

{
  "code": "schema_validation_failed ",
  "message": "Validation failed",
  "errors": [
    {
      "field": "grant_type",
      "message": "field is missing"
    }
  ]
}

我查看了以下内容,但无法理解。任何提示将不胜感激! How to modify token endpoint response body with Owin OAuth2 in Asp.Net Web API 2

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-web-api c#-4.0


    【解决方案1】:

    对于其他任何人,如果他们必须在 ASP.NET Web API 中自定义 OAuth 错误响应,特别是对于为空的 grant_type 字段,请在您的 ValidateClientAuthentication 方法中使用以下代码,理想情况下您可以收集客户端 ID 和机密值

        public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        {
                    string clientId;
                    string clientSecret;
                    if (!context.TryGetBasicCredentials(out clientId, out clientSecret))
                    {
                        context.TryGetFormCredentials(out clientId, out clientSecret);
                    }
                    
                    // Verifying if grant_type key exists in the token payload
                    if (context.Parameters.Get("grant_type") == null)
                    {
                        context.SetClientAuthError("schema_validation_failed", "Validation failed", "grant_type", "field is missing");
                        return;
                    }
         }
    

    【讨论】:

      猜你喜欢
      • 2017-09-25
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      • 1970-01-01
      • 2014-05-01
      • 2017-01-19
      • 2018-03-18
      相关资源
      最近更新 更多