【发布时间】:2018-10-14 14:08:39
【问题描述】:
我是 Dot net core 2 和实现 MVC 客户端和 IdentityServer4 的新手。
在获取外部用户访问令牌时面临两个问题。
问题 1
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
//options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
当添加下面的代码行时
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
即使在 ExternalLoginCallback
中成功验证后用户也无法登录问题 2
如果我删除了上面的代码行,用户可以登录但await HttpContext.GetTokenAsync("access_token")
返回 null。
这里是startup.cs的完整代码
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
//options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie()
.AddGoogle(googleOptions =>
{
googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];
googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
googleOptions.SaveTokens = true;
})
.AddOpenIdConnect(options =>
{
options.Authority = "http://localhost:xxx/";
options.RequireHttpsMetadata = false;
options.ClientId = "xxx";
options.ClientSecret = "xxx";
options.ResponseType = "code id_token";
options.Scope.Add("xxxx");
options.Scope.Add("email");
options.Scope.Add("offline_access");
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
});
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvcWithDefaultRoute();
}
我们将不胜感激。
【问题讨论】:
标签: .net-core asp.net-core-mvc access-token identityserver4