gpt4 book ai didi

c# - 如何使用 OWIN 中间件在 Web Api 中实现 OAuth2 Authorization_Code Flow?

转载 作者:可可西里 更新时间:2023-11-01 09:10:53 24 4
gpt4 key购买 nike

我正在尝试创建一个简单的支持 OAuth 的概念验证应用程序,但卡在授权代码实现上。我读到的所有地方似乎都以这样或那样的方式进行,从未真正使用过授权代码流。我一直在使用以下资源获取信息:

我已经设置了 web api 和带有自定义 OAuthAuthorizationServerProvider 的 owin 以接受刷新 token 的密码授予类型以及将刷新 token 交换为访问 token 的能力。这工作得很好,但我想设置一个场景,我将浏览器重定向到服务器以授权并使用授权代码重定向回客户端。然后我希望客户端将授权码发送到 token 端点以获取刷新 token

在 Web Server Apps 下的第二个链接中,我试图让我的 web api 应用程序显示来自请求的授权代码,例如 http://127.0.0.1/auth?response_type=code&client_id=123&redirect_uri=http://validredirect.com&scope=access ,但我一直收到 404。

我已经按如下方式配置了 owin:

var databaseContext = new AdnsfContext();

WebApp.Start(
new StartOptions("http://127.0.0.1:7000"),
appBuilder =>
{
var httpConfig = new HttpConfiguration();
httpConfig.MapHttpAttributeRoutes();
httpConfig.SuppressDefaultHostAuthentication();
httpConfig.Filters.Add(new HostAuthenticationFilter("Bearer"));

appBuilder
.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = true,
ApplicationCanDisplayErrors = true,
AuthorizeEndpointPath = new PathString("/auth"),
TokenEndpointPath = new PathString("/token"),
AuthorizationCodeExpireTimeSpan = TimeSpan.FromMinutes(1),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(1),
Provider = new AuthServerProvider(),
AuthorizationCodeProvider = new AuthorizationCodeProvider(),
RefreshTokenProvider = new RefreshTokenProvider(),
})
.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
{
AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
AuthenticationType = "Bearer",
})
.UseCors(CorsOptions.AllowAll)
.UseWebApi(httpConfig);
});

我为启用授权端点而添加的部分是身份验证服务器选项的属性:

AuthorizeEndpointPath = new PathString("/auth"),
AuthorizationCodeExpireTimeSpan = TimeSpan.FromMinutes(1),
AuthorizationCodeProvider = new AuthorizationCodeProvider(),

我的 AuthorizationCodeProvider 实现中的覆盖抛出了未实现的异常,但它目前甚至没有命中代码中设置的任何断点。需要注意的一件事是,当我使用 postman 访问 auth 端点时,我得到了一个 HTTPAPI/2.0 的服务器 header ,这与该端点上根本没有任何内容的情况不同,这意味着我必须发送请求不正确。任何人都可以看到我的设置有问题吗?在此先感谢,我知道这显然是我对 OWIN 和 OAuth 的理解失败。

最佳答案

Katana 中内置的 OAuth2 授权服务器需要注意的一件事是它透明:您必须提供自己的/auth 端点(例如使用 MVC 或 Nancy)或直接在OAuthAuthorizationServerProvider.AuthorizationEndpoint

可以看看https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/tree/dev/samples/Mvc一个完整的样本。它不使用 Katana 中内置的 OAuth2 授权服务器,而是使用针对 OpenID Connect 的更精细的分支,但您应该明白这一点。

关于c# - 如何使用 OWIN 中间件在 Web Api 中实现 OAuth2 Authorization_Code Flow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28749990/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com