簡體   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

我正在為我的 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());
    }

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

現在,在嘗試使用端點http://localhost:8080/token生成令牌時,我得到了 clientId 和 clientSecret 的 NULL ,因此我得到了"error": "invalid_client" 我在這里缺少什么?

在此處輸入圖像描述

在此處輸入圖像描述

編輯:編輯

當我使用raw作為正文時,我可以看到令牌生成正在工作,並且客戶端和秘密都具有價值。 為什么它不適用於form-data

在此處輸入圖像描述

查看 postman 文檔: 發送 API 請求

最重要的是:

網站 forms 經常將數據作為 multipart/form-data 發送到 API。 您可以使用表單數據正文選項卡在 Postman 中復制此內容。 表單數據允許您發送鍵值對,並指定內容類型。

通過快速搜索 web,需要對 API 進行特殊類型的處理以綁定multipart/form-data

如何為多部分/表單數據設置 Web API controller

甚至還有一個插件

內容類型很重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM