![](/img/trans.png)
[英]Validate IdentityServer4 token in Web API (.Net framework)
[英]Web Api .net framework 4.6.1 and identityServer4
Web Api .net 框架
我有一個使用 IdentityServer4 .net core 1.1 完成的身份驗證服務。 客戶端設置如下:
new Client
{
ClientId = "client",
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "api1" }
},
// resource owner password grant client
new Client
{
ClientId = "ro.client",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "api1" }
},
// OpenID Connect hybrid flow and client credentials client (MVC)
new Client
{
ClientId = "mvc",
ClientName = "MVC Client",
AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
RequireConsent = true,
ClientSecrets =
{
new Secret("secret".Sha256())
},
RedirectUris = { "http://localhost:5002/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"api1"
},
AllowOfflineAccess = true
},
// JavaScript Client
new Client
{
ClientId = "js",
ClientName = "JavaScript Client",
AllowedGrantTypes = GrantTypes.Implicit,
AllowAccessTokensViaBrowser = true,
RedirectUris = { "http://localhost/web/main.html#/redirectLogin#" },
PostLogoutRedirectUris = { "http://localhost/web" },
AllowedCorsOrigins = { "http://localhost" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"api1"
},
RequireConsent = false
}
我有一個使用 oidc-client 的帶有 javascript 的前端應用程序。 在其中,我可以使用以下設置向身份驗證服務器進行身份驗證:
var userManagerConfig = {
authority: "http://localhost:5000",
client_id: "js",
redirect_uri: "http://localhost/web/main.html#/redirectLogin#",
response_type: "id_token token",
scope: "openid profile api1",
post_logout_redirect_uri: "http://localhost/web",
};
var userManager = new Oidc.UserManager(userManagerConfig);
我還有一個用 .net framework 4.6.1 制作的 api web。 在其中我想從前端接收身份驗證並使用身份驗證服務器來驗證訪問。
這種情況應該如何設置?
您的 API 應該在 Identity Server 中注冊為 API 資源。 然后 - 它應該實現 OwinStartup 並在其中包含:
public void Configuration(IAppBuilder app)
{
// accept access tokens from identityserver and require a scope of 'api1'
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "<ids address>",
ValidationMode = ValidationMode.Both,
RequiredScopes = new[] { "myapi" }
});
// configure web api
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
app.UseWebApi(config);
}
而且,因為它是一個 .NET Framework API,所以它需要引用IdentityServer3.AccessTokenValidation
。 這不應該打擾您並引起任何擔憂。 它毫不猶豫地處理 IdentityServer4 令牌。
其他一切都是標准的 - 您需要在所有想要要求的控制器/方法上使用AuthorizeAttribute
或添加以下內容:
// require authentication for all controllers
config.Filters.Add(new AuthorizeAttribute());
在Startup.cs
並強制對所有控制器進行授權。
@m3n7alsnak3 的答案是正確的,我使用app.UseIdentityServerBearerTokenAuthentication
方法,但我收到未授權消息!
我的 webapi 是 .net 4.6.1,我的 IdentityServer4 是 .Net Core 3.1。
經過兩天的研發和閱讀IdentityServerBearerTokenValidationMiddleware
源代碼,我發現了這篇文章:
需要設置 IdentityServerOptions.AccessTokenJwtType
需要設置 IdentityServerOptions.EmitLegacyResourceAudienceClaim
最后我在我的IdentityServer
配置中添加了這個選項,它工作正常。
var builder = services.AddIdentityServer(option =>
{
option.AccessTokenJwtType = "";
option.EmitLegacyResourceAudienceClaim = true;
})
.AddInMemoryApiResources(Config.Apis)
.AddInMemoryClients(Config.Clients);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.