Asp.net core ASP.Net核心应用程序即使在实现IdentityServer后也会重定向到/Account/Login
我已经创建了一个简单的身份服务器,现在正在尝试对.Net核心应用程序进行身份验证。 即使在配置Startup.cs之后,当我运行解决方案时,系统仍然导航到/Account/Login; 我希望系统导航到Identity Server 下面是我的Startup.cs代码Asp.net core ASP.Net核心应用程序即使在实现IdentityServer后也会重定向到/Account/Login,asp.net-core,openid-connect,asp.net-core-2.2,Asp.net Core,Openid Connect,Asp.net Core 2.2,我已经创建了一个简单的身份服务器,现在正在尝试对.Net核心应用程序进行身份验证。 即使在配置Startup.cs之后,当我运行解决方案时,系统仍然导航到/Account/Login; 我希望系统导航到Identity Server 下面是我的Startup.cs代码 public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions
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.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultAuthenticateScheme = "oidc";
}).AddCookie(options =>
{
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
options.Cookie.Name = "identitycookie";
}).AddOpenIdConnect("oidc", options =>
{
options.Authority = "https://localhost:44123/identity";
options.ClientId = "wp7jfcxEHaRE8DUIZka";
options.ResponseType = "id_token token";
options.SaveTokens = true;
options.SignInScheme = "Cookies";
options.Configuration = new OpenIdConnectConfiguration
{
AuthorizationEndpoint =
"https://localhost:44123/identity/connect/authorize",
TokenEndpoint =
"https://localhost:44123/identity/connect/token"
};
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment 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.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc();
}
public void配置服务(IServiceCollection服务)
{
配置(选项=>
{
//此lambda确定给定请求是否需要非必要cookie的用户同意。
options.checkApprovered=context=>true;
options.MinimumSameSitePolicy=SameSiteMode.None;
});
services.AddAuthentication(选项=>
{
options.DefaultScheme=“Cookies”;
options.DefaultAuthenticateScheme=“oidc”;
}).AddCookie(选项=>
{
options.Cookie.SecurePolicy=CookieSecurePolicy.Always;
options.Cookie.Name=“identitycookie”;
}).AddOpenIdConnect(“oidc”,选项=>
{
选项。权限=”https://localhost:44123/identity";
options.ClientId=“wp7jfcxEHaRE8DUIZka”;
options.ResponseType=“id\u令牌”;
options.SaveTokens=true;
options.signnscheme=“Cookies”;
options.Configuration=新的OpenIdConnectConfiguration
{
授权端点=
"https://localhost:44123/identity/connect/authorize",
标记端点=
"https://localhost:44123/identity/connect/token"
};
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Error”);
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc();
}
有人能告诉我我做错了什么吗
提前感谢使用以下代码:
options.DefaultChallengeScheme = "oidc";
而不是:
options.DefaultAuthenticateScheme = "oidc";
这将挑战oidc模式,并使用户重定向到外部身份验证提供者。效果良好
另一种方法是设置CookieAuthenticationOptions.ForwardChallenge
。这样,即使在默认的“Cookies”方案上触发了质询,它也将被转发到“oidc”方案:
对不起,我对这个话题不太熟悉,请向上投票,这样你的问题就会被这个领域有智慧的人注意到。谢谢。。这解决了我的问题。@Shaam,请接受我的回答,这可能会帮助其他遇到同样问题的人。
.AddCookie(options =>
{
// [...]
options.ForwardChallenge = "oidc";
})