![](/img/trans.png)
[英]Using ASP.NET Core Identity with IdentityServer4 - Authentication
[英]IdentityServer4 Asp.Net Core Identity
我正在嘗試使用 Asp.Net Core Identity 創建基本的 IdentityServer4,如本教程中所示。
但是當我調用登錄方法時:
return Challenge(new AuthenticationProperties { RedirectUri = "/Home/Index" }, "oidc");
我收到 404 錯誤:
http://localhost:5000/account/login?returnUrl=%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3Dmvc%26redirect_uri%3Dhttps%253A%252F%252Flocalhost%253A44391%252Fsignin-oidc%26response_type%3Dcode%2520id_token%26scope%3Dopenid%2520profile%2520api1%26response_mode%3Dform_post%26nonce%3D636682993147514721.ZDA2MmI5ZTgtMWU3Yi00ZjMzLTkyODMtZjBiNWIzMjUzZTRjZmYxMjIxYWItOTk5NS00OGJlLWE0M2EtOTg3ZTYyM2EzZWVk%26state%3DCfDJ8D1ISazXjTpLmRDTOwhIeT5cVwo-oh7P4hDeZa0Q7cSfU6vKRDTEu3RHraTyz4Wb8oQngGo-qAkzinXV2yFJuqClVRB_1gwLLXIvVK4moxtgGjZUGUJIDmoqQHrQCOxGNJLrGkaBiS74vxd1el8N1wSseoSBlqZD94OlShI53wgPNKXPiDzT0FLOI47MNwHwzW0d5q0n752kZiVp2V31CZemI6wtaEgte3Mb9iouFzrSyAW5XaBMdDEnAGPCNZ2d5Zfgwb2Cmp61B-I9t05aDHqR-5cxYtr0PVVM6PwBKy-1olSFH8uIc8ku0UJn7PY0WA%26x-client-SKU%3DID_NETSTANDARD1_4%26x-client-ver%3D5.2.0.0
我需要任何額外的視圖和控制器嗎? 我認為將使用 Asp.Net Core Identity 中的內容。
我的 IdentityServer4 配置:
public void ConfigureServices(IServiceCollection services)
{
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;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddDefaultUI()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// configure identity server with in-memory stores, keys, clients and scopes
services.AddIdentityServer()
.AddSigningCredential("CN=tst")
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddAspNetIdentity<IdentityUser>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
//app.UseHttpsRedirection();
//app.UseCookiePolicy();
app.UseStaticFiles();
app.UseIdentityServer();
app.UseMvcWithDefaultRoute();
//app.UseMvc(routes =>
// {
// routes.MapRoute(
// name: "default",
// template: "{controller=Home}/{action=Index}/{id?}");
// });
}
我的客戶端配置:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication("oidc")
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", optins =>
{
optins.SignInScheme = "Cookies";
optins.Authority = "http://localhost:5000";
optins.RequireHttpsMetadata = false;
optins.ClientId = "mvc";
optins.ClientSecret = "secret";
optins.ResponseType = "code id_token";
optins.GetClaimsFromUserInfoEndpoint = true;
optins.Scope.Add("openid");
optins.Scope.Add("profile");
//optins.Scope.Add("email");
optins.Scope.Add("api1");
optins.ClaimActions.Add(new JsonKeyClaimAction("role", "role", "role"));
optins.SaveTokens = true;
});
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;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
IdentityServer4 控制台中沒有錯誤。
隨着 ASP.NET Core Identity 2.1 的引入,視圖、控制器等不再添加到使用 Visual Studio 或 dotnet CLI 生成的項目中。 相反,它們是通過Razor 類庫提供的。
作為此更改的一部分,舊式 URL,例如/Account/Login
(如您的示例中所示)也發生了更改。 這些現在以/Identity
為前綴,並通過位於名為Identity
的區域中的 ASP.NET Core Razor 頁面提供。
IdentityServer4 默認使用舊式 URLs ,正如我所說,它不再存在(導致您的 404)。 要解決此問題,請在代碼中配置IdentityServerOptions
對象以使用新位置:
services.AddIdentityServer(options =>
{
options.UserInteraction.LoginUrl = "/Identity/Account/Login";
options.UserInteraction.LogoutUrl = "/Identity/Account/Logout";
})
.AddSigningCredential("CN=tst")
// ...
只有兩個與身份相關的 URL 可以配置,因此為了完整起見,我在上面的代碼中都添加了它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.