简体   繁体   English

Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerProvider - clientId 和 clientSecret 在尝试从 Postman 生成令牌时出现 null

[英]Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerProvider - clientId and clientSecret coming null while trying to generate token from Postman

I am using OWIN security for my asp.net web api 2 application and here is my startup class setting for auth.我正在为我的 asp.net web api 2 应用程序使用 OWIN 安全性,这是我的启动 ZA2F2ED4F8EBC2CBBDC4 设置。

 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());
    }

And here is CustomAuthorizationServerProvider class implementation,这是CustomAuthorizationServerProvider class 实现,

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);
    }
}

Now, while trying to generate token using endpoint http://localhost:8080/token , I am getting NULL for both clientId and clientSecret and hence I am getting "error": "invalid_client" .现在,在尝试使用端点http://localhost:8080/token生成令牌时,我得到了 clientId 和 clientSecret 的 NULL ,因此我得到了"error": "invalid_client" What I am missing here?我在这里缺少什么?

在此处输入图像描述

在此处输入图像描述

Edit: EDIT编辑:编辑

When I am using raw as body, I can see token generation is working and both client and secret have value.当我使用raw作为正文时,我可以看到令牌生成正在工作,并且客户端和秘密都具有价值。 Why it is not working for form-data ?为什么它不适用于form-data

在此处输入图像描述

Check the postman documentation: Sending API requests查看 postman 文档: 发送 API 请求

Most importantly this:最重要的是:

Website forms often send data to APIs as multipart/form-data.网站 forms 经常将数据作为 multipart/form-data 发送到 API。 You can replicate this in Postman using the form-data Body tab.您可以使用表单数据正文选项卡在 Postman 中复制此内容。 Form data allows you to send key-value pairs, and specify the content type.表单数据允许您发送键值对,并指定内容类型。

With a quick search around the web, there needs to be a special type of handling for the APIs to bind the multipart/form-data通过快速搜索 web,需要对 API 进行特殊类型的处理以绑定multipart/form-data

ie How to set up a Web API controller for multipart/form-data如何为多部分/表单数据设置 Web API controller

There is even a plugin for that甚至还有一个插件

Content type is important.内容类型很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Microsoft.Owin.Security.Oauth承载令牌授权拦截 - Microsoft.Owin.Security.Oauth Bearer Token Authorization Interception 未设置OAuth2Client clientId和clientSecret - OAuth2Client clientId and clientSecret not set 健全性检查Microsoft Graph API和从Postman生成OAuth令牌 - Sanity Check Microsoft Graph API and Generating OAuth Token from Postman 通过asp.net Microsoft.Owin.Security.OAuth获取访问令牌 - get access token via asp.net Microsoft.Owin.Security.OAuth Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware警告:0:收到无效的承载令牌 - Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware Warning: 0 : invalid bearer token received Microsoft Bot Framework示例致文本Bot的语音-clientid和clientSecret - Microsoft Bot Framework Example Speech to Text Bot - clientid and clientSecret 从OWIN中的OAuth Bearer Token获取IPrincipal - Get IPrincipal from OAuth Bearer Token in OWIN ThinkTecture IdentityServer3与Microsoft.Owin.Security.OAuth2 - ThinkTecture IdentityServer3 vs Microsoft.Owin.Security.OAuth2 验证来自NodeJS的“ Owin OAuth承载令牌” - Validate “Owin OAuth Bearer Token” from NodeJS 从 AuthURL 和 ClientID 获取 OAuth2.0 Bearer Token - Get OAuth2.0 Bearer Token from AuthURL and ClientID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM