我有一个受Azure AD保护的api,我正尝试从MVC应用程序访问它。此MVC应用程序将IdentityServer4用作IDP,并将Azure AD配置为外部提供程序。我无法使用从Azure AD获得的具有访问令牌的API进行调用。
天蓝色配置
IdentityServer4配置:
Startup.cs-ConfigureServices
if [[ "$file" == "$file" ]]; then
我正在使用内存客户端进行测试
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddTestUsers(Config.GetUsers());
services.AddAuthentication()
.AddOpenIdConnect("oidc", "Azure AD", options =>
{
options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
options.SignOutScheme = IdentityServerConstants.SignoutScheme;
options.Authority = "https://login.microsoftonline.com/tenant.onmicrosoft.com/";
options.ClientId = "IDSRV";
options.Resource = "API"; //Identity server uses scopes but Azure AD expects this as a Resource. Not sure if this is the right way
options.ClientSecret = "secret from azure";
options.ResponseType = "code id_token";
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "role"
};
});
API资源配置:我猜这仅在同意屏幕中使用
public static IEnumerable<Client> GetClients()
{
return new List<Client>
{
new Client
{
ClientId = "mvc",
ClientName = "MVC Client",
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedGrantTypes = GrantTypes.Hybrid,
RedirectUris = { "http://localhost:49341/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:49341/signout-callback-oidc" },
AllowedScopes = new List<string>
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"API" //assume this is the appId from Azure
}
}
};
}
Web客户端配置:(这是Identiserver4的客户端,需要访问API)
Startup.cs-ConfigureServices
public static IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>
{
new ApiResource("API","Test API")
};
}
身份验证中没有错误,但是如果我尝试使用获得的访问权限来访问API,则会出现未经授权的错误。
我能够直接使用Azure Ad设置另一个测试客户端,并使用收到的令牌访问API。
现在我有两个问题: