【发布时间】:2018-05-22 17:32:17
【问题描述】:
我想为我的网络应用程序设置一个过期时间,以便在 1 小时后用户将自动注销。似乎 azure 现在有了一个名为“条件访问”的新功能。首先,我必须有一个高级帐户(所以还有更多的钱),其次,我在文档中找不到任何内容,说明如何使用它在指定时间后将某人注销。有人用过这个功能吗?这是怎么做到的?
谁能帮忙?
【问题讨论】:
标签: azure azure-active-directory access-token logout
我想为我的网络应用程序设置一个过期时间,以便在 1 小时后用户将自动注销。似乎 azure 现在有了一个名为“条件访问”的新功能。首先,我必须有一个高级帐户(所以还有更多的钱),其次,我在文档中找不到任何内容,说明如何使用它在指定时间后将某人注销。有人用过这个功能吗?这是怎么做到的?
谁能帮忙?
【问题讨论】:
标签: azure azure-active-directory access-token logout
假设您正在使用 OpenID Connect 和 Cookie 身份验证中间件来保护您的 Web 应用程序,根据您的要求,我假设您可以添加一个名为 loggedTicks 的自定义声明并检查 @987654323 的 OnValidateIdentity 下的时间间隔@,然后针对您的 Web 应用程序和 AAD 显式调用注销操作。这里是sn-p的代码,大家可以参考一下:
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions() {
ExpireTimeSpan=TimeSpan.MaxValue,
Provider = new CookieAuthenticationProvider()
{
OnValidateIdentity = ctx => {
var loggedClaim=ctx.Identity.FindFirst("loggedTicks")?.Value;
if (loggedClaim != null)
{
var loggedDateTime = new DateTime(long.Parse(loggedClaim), DateTimeKind.Utc);
if (loggedDateTime.AddHours(1) < DateTime.UtcNow)
{
ctx.RejectIdentity();
ctx.OwinContext.Authentication.SignOut(OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
}
}
return Task.FromResult(0);
}
}
});
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
RedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = context =>
{
context.HandleResponse();
context.Response.Redirect("/Error?message=" + context.Exception.Message);
return Task.FromResult(0);
},
SecurityTokenValidated = async (x) =>
{
var identity = x.AuthenticationTicket.Identity;
//add a additional claim which represents the current user logged UTC time ticks
identity.AddClaim(new System.Security.Claims.Claim("loggedTicks", DateTime.UtcNow.Ticks.ToString()));
await Task.FromResult(0);
}
}
});
}
【讨论】:
loggedTicks 声明,然后您可以将客户端用户重定向到需要授权访问的特定页面。