【问题标题】:azure conditional access logout after a specified time天蓝色条件访问在指定时间后注销
【发布时间】:2018-05-22 17:32:17
【问题描述】:

我想为我的网络应用程序设置一个过期时间,以便在 1 小时后用户将自动注销。似乎 azure 现在有了一个名为“条件访问”的新功能。首先,我必须有一个高级帐户(所以还有更多的钱),其次,我在文档中找不到任何内容,说明如何使用它在指定时间后将某人注销。有人用过这个功能吗?这是怎么做到的?

谁能帮忙?

【问题讨论】:

    标签: azure azure-active-directory access-token logout


    【解决方案1】:

    假设您正在使用 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);
                        }
                }
            });
    } 
    

    【讨论】:

    • 非常感谢您,帮了大忙。不过,只有一个小问题,如何在没有用户交互的情况下强制注销并进入注销屏幕?目前,在预定的注销时间之后,注销过程本身仅在用户与站点进行一些交互时才起作用,即单击菜单项等,因此尽管注销时间已过,但屏幕仍显示他们的最后一页访问过
    • 对于自动注销,我假设您可以连续向匿名访问的操作发送 ajax 请求,如果验证失败,该操作将在指定的时间间隔(例如 5 秒)内验证 loggedTicks 声明,然后您可以将客户端用户重定向到需要授权访问的特定页面。
    猜你喜欢
    • 2020-09-24
    • 2016-01-30
    • 1970-01-01
    • 2021-05-14
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多