我知道这个问题在不同的帖子里讨论过,但我没有找到问题的答案。
我正在用.net core 3.1和IdentityServer4做一个POC。
目前,我有一个带有IdentityServer4的Auth服务器和一个.net core的控制台应用程序。这个组合可以工作,我没有问题。
下一步是做一个空白的网站,看看身份认证与IdentityServer4是如何工作的。我在我的Auth服务器中添加了一个客户端,也做了一个网站,当我试图访问一个有模型[Authorize]的页面时,我得到了这样的消息:。
对不起,出现了一个错误:invalid_scope。
无效范围
这是我设置客户的地方。
public static IEnumerable<Client> GetClients()
{
return new List<Client>
{
new Client
{
ClientId = "consoleappclient",
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "bxsecurityapi" }
},
// OpenID Connect implicit flow client (MVC)
new Client
{
ClientId = "razorappclient",
ClientName = "Razor Client",
AllowedGrantTypes = GrantTypes.Implicit,
RedirectUris = { "https://localhost:5005/signin-oidc" },
PostLogoutRedirectUris = { "https://localhost:5005/signout-callback-oidc" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
}
}
};
}
这是我的客户。
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = "Cookies";
options.Authority = "https://localhost:5001";
options.RequireHttpsMetadata = false;
options.Scope.Add("openid");
options.ClientId = "razorappclient";
options.SaveTokens = true;
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
先谢谢你
bxsecurityapi,这也应该被添加到客户端的范围中。然后只有身份服务器会响应。详情请查看 identityserver4 文档,里面有详细的描述。
我的错...
我在我的Auth服务器上有这一行。
.AddInMemoryIdentityResources(Config.GetApiResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
正确的做法是...
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
我在这里发布的是好的,事实上。对于日志文件来说,真的是个好主意。它还没有实现,但它真的很容易,帮了我很多。
谢谢大家
无效的作用域与您的作用域名称有关,请确保您定义的作用域与客户端的作用域正确匹配。bxsecurityapi
或 OpenId
的作用域,而该作用域与客户的不匹配。openid
!
Edited:所以问题出在你的标准作用域上,试试这个。
options.Scope.Add("openid profile email");
或者尝试在你的服务器端应用程序中添加一个新的作用域名称。
new Client
{
ClientId = "razorappclient",
ClientName = "Razor Client",
AllowedGrantTypes = GrantTypes.Implicit,
RedirectUris = { "https://localhost:5005/signin-oidc" },
PostLogoutRedirectUris = { "https://localhost:5005/signout-callback-oidc" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
"openIdTest",
}
}
然后在你的客户端应用程序中使用它,它应该能解决你的问题。