繁体   English   中英

带OAuth应用程序流的SwashBuckle.AspNetCore 4.1?

[英]SwashBuckle.AspNetCore 4.1 w/ OAuth application flow?

我正在尝试使用带有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。

如果选中Fiddler,您将看到client_id和client_secret在授权下的请求标头中。 这是字符串Basic,后跟您的用户名和密码(以64为基数)的字符串。

就像是

Authorization: Basic MTIzOmFiYw==

您的令牌方法需要做类似的事情

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM