会话超时重定向用于Azure AD身份验证

时间:2020-09-01 13:27:31

标签: c# asp.net-core session azure-active-directory

我正在使用Azure AD在Asp .Net Core 3.1应用程序中进行身份验证。我还需要处理我设置的某些会话变量的会话超时。我不知道如何将它们联系在一起。我创建了一个名为SessionTimeout的过滤器操作来注释我的控制器,但是由于会话不足,我不知道应该重定向到哪里。

这是我的“过滤器”操作:

public class SessionTimeout : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            if (filterContext.HttpContext.Session == null ||
                                !filterContext.HttpContext.Session.TryGetValue("UserId", out byte[] val))
            {
                filterContext.Result =
                    new RedirectToRouteResult(new RouteValueDictionary(new
                    {
                        controller = "Home",
                        action = "Login"
                    }));
            }
            base.OnActionExecuting(filterContext);
        }
    
    }

但是使用AzureAD时当然不存在Home / Login,因此我不知道应该重定向到哪里。而且他们不必重新进行身份验证,只需重新建立会话变量即可,所以我不确定应该做什么。

1 个答案:

答案 0 :(得分:0)

我认为您不需要创建ActionFilterYou can protect a controller or controller methods using the [Authorize] attribute

您可以在自己的对象中services.AddSession。有关更多详细信息,您可以阅读Session and state management in ASP.NET Core

我还发现了a similar post,但是它基于.net core 2.0,但是它对您仍然有用。

enter image description here