.Net Web 服务与 oauth2 仅响应未经授权的_client (400)

问题描述 投票:0回答:1

我正在尝试获得一个简单的网络服务来使用 oauth2 客户端凭据,但在尝试获取令牌时该服务以未经授权的_client 进行响应,我已经搜索并尝试了所有我能找到的方法,但没有成功。 我的目的是让服务使用令牌进行响应,客户端将使用该令牌从服务中获取数据。 我正在使用 .Net Framework 4.8。

配置

OAuthOptions = new OAuthAuthorizationServerOptions
{
    TokenEndpointPath = new PathString("/token"),
    Provider = new OAuthAppProvider(),
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(2),
    AllowInsecureHttp = true
};

这是我的验证方法,当使用断点时,我可以看到客户端 ID 和密钥已验证,并且程序调用 context.Validated()。

     public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
     {
         string clientId;
         string clientSecret;           

         //first try to get the client details from the Authorization Basic header
         if (!context.TryGetBasicCredentials(out clientId, out clientSecret))
         {
             //no details in the Authorization Header so try to find matching post values
             context.TryGetFormCredentials(out clientId, out clientSecret);
         }

         if (string.IsNullOrWhiteSpace(clientId) || string.IsNullOrWhiteSpace(clientSecret))
         {
             context.SetError("client_not_authorized", "invalid client details");
             return Task.FromResult<object>(null);
         }

         if (clientId.Equals("ZYDPLLBWSK3MVQJSIYHB1OR2JXCY0X2C5UJ2QAR2MAAIT5Q") && clientSecret.Equals("my-secret-password"))
         {
             context.Validated();            
             return Task.FromResult<object>(null);
         }
         context.SetError("unauthorized_client", "unauthorized client");
         return Task.FromResult<object>(null);            
     }

我使用 Postman 作为测试客户端

[邮递员屏幕截图] (https://i.stack.imgur.com/ffUsf.png)

如屏幕截图所示,服务器响应 400,但没有令牌。 我完全没有想法,无法抓住提示。

c# oauth-2.0 oauth
1个回答
0
投票

发现我的问题,我还需要重写我的提供程序类中的 GrantClientCredentials 。有点奇怪,我搜索时没有找到任何这样的例子。

这是我的提供者课程:

  public class OAuthAppProvider : OAuthAuthorizationServerProvider

{ 公共覆盖任务 ValidateClientAuthentication(OAuthValidateClientAuthenticationContext 上下文) { //首先尝试从授权基本标头获取客户端详细信息 if (!context.TryGetBasicCredentials(输出字符串 clientId, 输出字符串 clientSecret)) { //授权标头中没有详细信息,因此尝试查找匹配的帖子值 context.TryGetFormCredentials(out clientId, out clientSecret); }

      if (string.IsNullOrWhiteSpace(clientId) || string.IsNullOrWhiteSpace(clientSecret))
      {
          context.SetError("client_not_authorized", "invalid client details");
          return Task.FromResult<object>(null);
      }
      
      if (ValidateClient(clientId, clientSecret))
      {
          context.Validated();            
          return Task.FromResult<object>(null);
      }
      context.SetError("unauthorized_client", "unauthorized client");
      return Task.FromResult<object>(null);            
  }

  private bool ValidateClient(string clientId, string clientSecret)
  {
      return ConfigurationManager.AppSettings["ClientId"].ToUpper() == clientId.ToString().ToUpper() && 
          ConfigurationManager.AppSettings["ClientSecret"].ToUpper() == clientSecret.ToString().ToUpper();
  }

  public override async Task GrantClientCredentials(OAuthGrantClientCredentialsContext context)
  {
      var claimsIdentity = new ClaimsIdentity(context.Options.AuthenticationType);
      claimsIdentity.AddClaim(new Claim("LoggedOn", DateTime.Now.ToString()));
      await Task.Run(() => context.Validated(claimsIdentity));
  }

}

© www.soinside.com 2019 - 2024. All rights reserved.