gpt4 book ai didi

c# - OAuth 2.0 不适用于本地 IIS

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:39 25 4
gpt4 key购买 nike

我正在为我的应用程序执行 OAuth 2.0 身份验证(客户端凭据流)。我有 Identity Server、ASP.NET Web Api 作为我要对其进行身份验证的应用程序和控制台应用程序作为客户端应用程序。

当我的 Web Api 托管在 IIS Express 上时,一切正常。当我将其更改为本地 IIS 时,它不起作用。在 IIS Express 上,我得到 ClaimsIdentity 作为请求主体,但在本地 IIS 上,我得到 WindowsIdentity,它没有声明。

我正在为 OAuth 使用 Thinktecture 库,为配置使用 Owin。这就是我的 Startup 类的样子:

public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = ConfigurationManager.AppSettings["Authority"],
RequiredScopes = new[] { "api" }
});
}
}

这是我检查主体身份的方式:

var claims = ((ClaimsIdentity)p.Identity).Claims;
var roleClaim = claims.Where(i => i.Type == "client_role").FirstOrDefault();
if (roleClaim == null)
{
return null;
}

当然,当我在本地 IIS 上时,我将返回 null,因为此身份没有任何声明。

当我更改 IIS 时,为什么我的应用会更改行为?我试图更改 IIS 配置中的某些内容,但老实说,我这样做相当盲目。

有人知道是什么导致了这里的问题吗?

编辑:我还将展示我如何获取 token 和发送请求,也许这是导致问题的原因。

private static TokenResponse GetToken()
{
var client = new OAuth2Client(new Uri(IdentityServerUri), clientId,clientScope);
return client.RequestClientCredentialsAsync("scope").Result;
}

主要功能:

var client = new HttpClient();
var tokenResponse = GetToken();
client.SetBearerToken(tokenResponse.AccessToken);
var result = client.GetStringAsync(requestUri).Result;

最佳答案

在 IIS 中检查您是否将身份验证设置为无。听起来 Windows 身份验证模块已启用。试试这个 article

关于c# - OAuth 2.0 不适用于本地 IIS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31672570/

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