gpt4 book ai didi

identityserver3 - IdentityServer4 IdentityServer3.AccessTokenValidation

转载 作者:行者123 更新时间:2023-12-04 06:57:50 27 4
gpt4 key购买 nike

祝大家新年快乐...

我配置了一个 IdentityServer4,我可以成功调用 ASP.net Core web api。
但是对于asp.net framework 4.5.2 web apis,
我从 .NET 框架 web api 中收到 {"响应状态代码不表示成功:401(未经授权)。"} 错误。我想请教您的帮助和意见。

我用 IS4 搜索了这个主题,并找到了一些关于 IdentityServer3.AccessTokenValidation 兼容性的条目。根据回复,我加载了签名证书并调用了 AddSigningCredential 而不是 AddTemporarySigninCredential。 x509certificate 是本地创建的证书。我将 IdentityServer3.AccessTokenValidation 版本更新为 v2.13.0。

我仍然收到错误。
任何帮助表示赞赏。

问候和感谢您的巨大努力。

IdentityServer 4 端:
启动文件

public void ConfigureServices(IServiceCollection services)
{
services
.AddIdentityServer()
//.AddTemporarySigningCredential()
.AddSigningCredential(x509Certificate)
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddAspNetIdentity<ApplicationUser>();
}

配置文件
    public static IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>
{
new ApiResource("AuthorizationWebApi","Authorization Web API .NET Core"),
new ApiResource("AuthorizationWebApiNetFramework","Authorization Web API NET Framework"),
new ApiResource("api1", "Empty Test Api")
};

}

public static IEnumerable<Client> GetClients()
{
return new List<Client> {
new Client {
ClientId = "silicon",
ClientName = "console app",
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets = { new Secret("abcdef".Sha256())},
AllowedScopes = new List<string>{
"AuthorizationWebApiNetFramework"
}

},
new Client
{
ClientId = "MYUX",
ClientName = "MYUX MVC Client",
AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
RequireConsent = false,
ClientSecrets= {new Secret("abcdef".Sha256()) },
RedirectUris = { "http://localhost:5002/signin-oidc" },
PostLogoutRedirectUris = {"http://localhost:5002"},

AllowedScopes = {
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"custom.profile",
"AuthorizationWebApi",
"AuthorizationWebApiNetFramework"
},
AllowOfflineAccess = true
}
};
}

.NET Framework API 端
public void Configuration(IAppBuilder app)
{
//ConfigureAuth(app);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "http://www.abcdefgh.com:5000",
ValidationMode = ValidationMode.ValidationEndpoint,
RequiredScopes = new[] { "AuthorizationWebApiNETFramework" }

});
//configure web api
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();

//require authentication for all controllers

config.Filters.Add(new AuthorizeAttribute());

app.UseWebApi(config);
}

调用方:
try
{
ViewData["Message"] = "Authorization Test.";
var accessToken = await HttpContext.Authentication.GetTokenAsync("access_token");
var authorizationApiClient = new HttpClient();
authorizationApiClient.SetBearerToken(accessToken);
var content = await authorizationApiClient.GetStringAsync("http://localhost:13243/values");
return View();
}
catch (Exception ex)
{
throw;
}

或通过控制台应用程序...
try
{
// discover endpoints from metadata
var disco = await DiscoveryClient.GetAsync("http://www.abcdefgh.com:5000");

var tokenClient = new TokenClient(disco.TokenEndpoint, "silicon", "abcdef");
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("AuthorizationWebApiNetFramework");

if (tokenResponse.IsError)
{
Console.WriteLine(tokenResponse.Error);
return;
}

Console.WriteLine(tokenResponse.Json);

var client = new HttpClient();
client.SetBearerToken(tokenResponse.AccessToken);

var response = await client.GetAsync("http://localhost:13243/values");
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response.StatusCode);
}
else
{
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine(JArray.Parse(content));
}
}
catch (Exception)
{
throw;
}

编辑:在 4.5.2 Api 方面:我注释掉了这一行
ValidationMode = ValidationMode.ValidationEndpoint。我按照 IS3 文档添加了这一行。谢谢大家。

最佳答案

删除 WebAPI 访问 token 验证中间件中的以下行。

ValidationMode = ValidationMode.ValidationEndpoint

结果应如下所示:
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions 
{
Authority = "http://www.abcdefgh.com:5000",
RequiredScopes = new[] { "AuthorizationWebApiNETFramework" }
});

关于identityserver3 - IdentityServer4 IdentityServer3.AccessTokenValidation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41425163/

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