.NetコアでIdentityServer 4認証を設定する方法を知っています。つまり:で定義された拡張を使用します。そして、私はそうのように私の起動クラスでそれを設定します:Identity Server 4の.net weapapi 2(コアではない)のAccessToken検証を設定します。
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = "http://localhost:5000",
RequireHttpsMetadata = false,
ApiName = "webapi"
});
の問題は、今私は、.NET 4.6のWeb API2(ないコア)への認証要求を行う必要があるということです。そして、same packageはそのためには機能しません。
this questionによると、Identity server 3:IdentityServer3.AccessTokenValidationで使用されていたのと同じパッケージを使用するだけです。
しかし、試してみると、Web APIにリクエストをすると401が表示されます。そして、認証イベントを結び付けてその背後にある理由を理解する方法はわかりません。ここに私の設定です:
アピStartup.cs:
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "http://localhost:5000",
RequiredScopes = new[] { "webapi" },
});
クライアントStartup.cs:
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
AuthenticationType = "oidc",
SignInAsAuthenticationType = "Cookies",
Authority = "http://localhost:5000",
RedirectUri = "http://localhost:3954/signin-oidc",
ClientId = "MvcClient",
Scope = "openid profile webapi offline_access",
ResponseType = "code id_token",
ClientSecret = "secret",
UseTokenLifetime = false,
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
},
});
クライアントプロジェクトでTestController:
var tokenClient = new TokenClient("http://localhost:5000/connect/token", "MvcClient", "secret");
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("webapi");
var client = new HttpClient();
client.SetBearerToken(tokenResponse.AccessToken);
var content = await client.GetStringAsync("http://localhost:5004/api/identity");
私が正常に取得ここにアクセストークン。しかし、api/identity
にリクエストをするときは401を取得します。ここで
は、IDPでconfigです:
new ApiResource("webapi", "My API")
[...]
new Client
{
ClientId = "MvcClient",
ClientName = "MVC Client",
AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
RequireConsent = true,
ClientSecrets =
{
new Secret("secret".Sha256())
},
RedirectUris = { "http://localhost:3954/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:3954/signout-callback-oidc" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"netcoremvcapi",
"webapi"
},
AllowOfflineAccess = true,
}
これが失敗する理由を任意のアイデア? IdentityServer3.AccessTokenValidationを使用してトークンを検証できるという誤った仮定をしていますか?