Razor .net core 3.1 with IdentityServer4



我知道这个主题在不同的帖子中讨论过,但我没有找到问题的答案。

我正在使用.net core 3.1和IdentityServer4进行POC。

目前,我有一个带有IdentityServer4的身份验证服务器和一个.net核心中的控制台应用程序。这种组合有效,我没有问题。

下一步是创建一个空白网站,以查看身份验证如何与 IdentityServer4 配合使用。我已经在我的身份验证服务器中添加了一个客户端并做了网站,当我尝试访问带有模型的页面时 [授权] 我收到消息:。

抱歉,出现错误: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 这也应该作为作用域添加到客户端中。然后只有身份服务器会响应。详细信息检查身份服务器 4 文档。有很好的描述。

我的坏...

我的身份验证服务器中有那行:

.AddInMemoryIdentityResources(Config.GetApiResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())

正确的事情是...

.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())

事实上,我在这里发布的内容很好。 日志文件真是个好主意。它还没有实现,但它真的很容易,对我帮助很大。

谢谢大家

与范围名称相关的范围无效,请确保定义的范围与客户端范围正确匹配。 正确地,您允许与客户端不匹配的bxsecurityapiOpenId范围openid

编辑: 所以问题是你的标准范围, 试试这个:

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",
}
}

然后在客户端应用程序中使用它,它应该可以解决您的问题。

最新更新