【问题标题】:SwashBuckle.AspNetCore 4.1 w/ OAuth application flow?SwashBuckle.AspNetCore 4.1 w/OAuth 应用程序流程?
【发布时间】:2019-08-17 10:12:56
【问题描述】:

我正在尝试使用带有 OAuth 应用程序流的 SwashBuckle.AspNetCore 4.1。根据 Google 搜索,我的设置如下所示:

            options.AddSecurityDefinition("oauth2", new OAuth2Scheme
            {
                Type = "oauth2",
                Flow = "application",
                TokenUrl = "/token",
            });

这为我提供了带有 client_id 和 client_secret 文本框的授权对话框,但是当我在提琴手中查看请求时,我看到了:

{"client_id":["client_id 字段是必需的。"],"client_secret":["client_secret 字段是必需的。"]}

使用“密码”流程,它同时显示用户名/密码和 client_id、client_secret 文本框并传入填充的对,但它始终传递不适合 client_id/secret 的密码授权。

【问题讨论】:

    标签: c# oauth-2.0 .net-core swagger swagger-ui


    【解决方案1】:

    如果您检查 Fiddler,您会看到 client_id 和 client_secret 在 Authorisation 下的请求标头中。这将是字符串 Basic 后跟您的用户名和密码 base 64 字符串编码。

    有点像

    Authorization: Basic MTIzOmFiYw==
    

    您的 Token 方法需要执行类似的操作

    string authHeader = Request.Headers["Authorization"];
    if (authHeader != null && authHeader.StartsWith("Basic"))
    {
        string encodedUsernamePassword = authHeader.Substring("Basic ".Length).Trim();
        Encoding encoding = Encoding.GetEncoding("iso-8859-1");
        string usernamePassword = encoding.GetString(Convert.FromBase64String(encodedUsernamePassword));
    
         int seperatorIndex = usernamePassword.IndexOf(':');
    
         var clientId = usernamePassword.Substring(0, seperatorIndex);
         var clientSecret = usernamePassword.Substring(seperatorIndex + 1);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-28
      • 2015-11-17
      • 2018-01-08
      • 2016-05-11
      • 2017-06-26
      • 2016-01-31
      • 2015-02-07
      • 2015-02-21
      • 2021-08-12
      相关资源
      最近更新 更多