【问题标题】:Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerProvider - clientId and clientSecret coming null while trying to generate token from PostmanMicrosoft.Owin.Security.OAuth.OAuthAuthorizationServerProvider - 尝试从 Postman 生成令牌时,clientId 和 clientSecret 为空
【发布时间】:2020-04-28 06:36:03
【问题描述】:

我正在为我的 asp.net web api 2 应用程序使用 OWIN 安全性,这是我的身份验证启动类设置。

 public void ConfigureOAuth(IAppBuilder app)
    {

        var oAuthServerOptions = new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = new CustomAuthorizationServerProvider()
        };

        // Token Generation
        app.UseOAuthAuthorizationServer(oAuthServerOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
    }

这里是CustomAuthorizationServerProvider 类的实现,

public class CustomAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
    public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
        context.TryGetFormCredentials(out var clientId, out var clientSecret);

        if (clientId == "987459827985" && clientSecret == "lkfjldsfjkld")
        {
            context.Validated(clientId);
        }

        return base.ValidateClientAuthentication(context);
    }

    public override Task GrantClientCredentials(OAuthGrantClientCredentialsContext context)
    {
        var oAuthIdentity = new ClaimsIdentity(context.Options.AuthenticationType);
        oAuthIdentity.AddClaim(new Claim(ClaimTypes.Name, "TestClient"));
        var ticket = new AuthenticationTicket(oAuthIdentity, new AuthenticationProperties());
        context.Validated(ticket);
        return base.GrantClientCredentials(context);
    }
}

现在,在尝试使用端点 http://localhost:8080/token 生成令牌时,我的 clientId 和 clientSecret 都为 NULL,因此我得到了 "error": "invalid_client"。我在这里缺少什么?

编辑:编辑

当我使用raw 作为正文时,我可以看到令牌生成正在工作,并且客户端和机密都具有价值。为什么它不适用于form-data

【问题讨论】:

标签: c# asp.net-web-api2 owin


【解决方案1】:

查看邮递员文档:Sending API requests

最重要的是:

网站表单通常将数据作为 multipart/form-data 发送到 API。您可以使用表单数据正文选项卡在 Postman 中复制此内容。表单数据允许您发送键值对,并指定内容类型。

通过在网络上进行快速搜索,需要对 API 进行特殊类型的处理以绑定 multipart/form-data

How to set up a Web API controller for multipart/form-data

There is even a plugin for that

内容类型很重要。

【讨论】:

  • 谢谢。我明白了。
猜你喜欢
  • 2012-08-01
  • 2019-11-19
  • 2017-09-01
  • 2017-10-07
  • 1970-01-01
  • 2021-07-21
  • 2015-07-20
  • 2014-09-03
  • 1970-01-01
相关资源
最近更新 更多