【发布时间】:2022-01-25 02:59:49
【问题描述】:
我们的应用程序正在使用 SSO(单点登录)功能进行用户身份验证,该功能正在检查来自 Azure Active Directory 的用户身份。
我们在Startup.cs类的Configure Services()方法中配置了一个SSO,这里我们使用AddCookie(),并将过期时间设置为29分钟。
根据我的理解,使用Ouath2.0 的 SSO 实现会在设置的 cookie 过期空闲时间段结束后自动重定向用户。
我在这里可能错了,因为它没有按预期工作,即使经过数小时的空闲时间,会话也不会过期。
任何人都可以检查下面的代码实现并提出更改以实现所需的功能吗?
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
//configure my sso
MySsoProvider.ConfigureSso(services, Configuration);
services.AddControllersWithViews().AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
//initialize azure provider
services.AddTransient(CreateAzureBlobFileProvider);
//ad app insight telemetry
services.AddApplicationInsightsTelemetry();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/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.UseRouting();
app.Use((context, next) =>
{
context.Request.Scheme = "https";
return next();
});
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
AzureStorageAccount.Load(Configuration);
}
MySsoProvider.cs:
public static void ConfigureSso(IServiceCollection services, IConfiguration Configuration)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = "PingCookie";
options.DefaultSignInScheme = "PingCookie";
options.DefaultChallengeScheme = "Ping";
}).AddCookie("PingCookie", options =>
{
options.Cookie.MaxAge = new TimeSpan(0, 29, 0);
options.ExpireTimeSpan = new TimeSpan(0, 29, 0); //cookie exipration set to 29 minutes
}).AddOAuth("Ping", options =>
{
options.ClientId = Configuration["abc-def-sso-clientid"];
options.ClientSecret = Configuration["abc-def-sso-secret"];
options.CallbackPath = new PathString("/signin-ping");
options.Scope.Add("openid profile email");
options.UsePkce = true;
options.SaveTokens = true;
options.AuthorizationEndpoint = Configuration["Ping:AuthorizationEndpoint"];
options.TokenEndpoint = Configuration["Ping:TokenEndpoint"];
options.UserInformationEndpoint = Configuration["Ping:UserInformationEndpoint"];
options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "immutable_id");
options.ClaimActions.MapJsonKey(ClaimTypes.GivenName, "given_name");
options.ClaimActions.MapJsonKey(ClaimTypes.Surname, "family_name");
options.ClaimActions.MapJsonKey(ClaimTypes.Email, "email");
options.ClaimActions.MapJsonKey("urn:openid:groups", "groups");
options.Events = new OAuthEvents
{
OnCreatingTicket = async context =>
{
var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
var response = await context.Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted);
response.EnsureSuccessStatusCode();
var responseText = await response.Content.ReadAsStringAsync();
var user = JsonDocument.Parse(responseText);
context.RunClaimActions(user.RootElement);
}
};
});
}
所需功能:
- 我们正在寻找用户在以下情况下自动重定向到其他页面 应用程序空闲 30 分钟(用户不执行任何操作)
【问题讨论】:
标签: c# asp.net-core oauth-2.0 azure-active-directory session-cookies