带有 IdentityServer4 的 Razor .net 核心 3.1
Razor .net core 3.1 with IdentityServer4
我知道这个主题在不同的帖子中讨论过,但我没有找到问题的答案。
我正在使用 .net core 3.1 和 IdentityServer4 进行 POC。
目前我有一个带有 IdentityServer4 的 Auth 服务器和一个 .net 核心中的控制台应用程序。这个组合有效,我没问题。
下一步是做一个空白网站,看看身份验证如何与 IdentityServer4 一起工作。我已经在我的 Auth 服务器中添加了一个客户端并且也创建了网站,当我尝试访问带有模型 [Authorize] 的页面时,我收到消息 :.
Sorry, there was an error : invalid_scope
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
或 OpenId
范围与客户端的 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",
}
}
然后在您的客户端应用程序中使用它,它应该可以解决您的问题。
bxsecurityapi 这也应该添加为客户端中的范围。然后只有身份服务器会响应。有关详细信息,请检查 identityserver4 documentation.there 描述得很好。
我的坏...
我的 Auth 服务器中有该行:
.AddInMemoryIdentityResources(Config.GetApiResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
正确的是...
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
我这里发的其实不错。
日志文件真的是个好主意。虽然还没有实现,但是真的很简单,对我帮助很大。
谢谢大家
我知道这个主题在不同的帖子中讨论过,但我没有找到问题的答案。
我正在使用 .net core 3.1 和 IdentityServer4 进行 POC。
目前我有一个带有 IdentityServer4 的 Auth 服务器和一个 .net 核心中的控制台应用程序。这个组合有效,我没问题。
下一步是做一个空白网站,看看身份验证如何与 IdentityServer4 一起工作。我已经在我的 Auth 服务器中添加了一个客户端并且也创建了网站,当我尝试访问带有模型 [Authorize] 的页面时,我收到消息 :.
Sorry, there was an error : invalid_scope
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
或 OpenId
范围与客户端的 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",
}
}
然后在您的客户端应用程序中使用它,它应该可以解决您的问题。
bxsecurityapi 这也应该添加为客户端中的范围。然后只有身份服务器会响应。有关详细信息,请检查 identityserver4 documentation.there 描述得很好。
我的坏...
我的 Auth 服务器中有该行:
.AddInMemoryIdentityResources(Config.GetApiResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
正确的是...
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
我这里发的其实不错。 日志文件真的是个好主意。虽然还没有实现,但是真的很简单,对我帮助很大。
谢谢大家