MVC自定义授权在实时服务器上一段时间后自动停止

时间:2017-06-13 10:32:35

标签: asp.net-mvc-4 authentication authorization asp.net-identity

我创建了asp .net mvc网站并创建了自定义身份验证和授权功能。此功能可以使用一段时间,但在一定时间后停止工作。有人可以请帮助。

用户只能访问受限区域时没有错误。

我的自定义代码:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public class CustomAuthrizationPayment : AuthorizeAttribute
    {
        bool authorize = false;
        bool IsLoggedIn = false;
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {

            string currentUserId = HttpContext.Current.User.Identity.Name;
            if (!string.IsNullOrEmpty(currentUserId))
            {
                using (IACLBEntities context = new IACLBEntities())
                {
                    var userStatus = context.AspNetUsers.FirstOrDefault(r => r.Email == currentUserId).IsActive;
                    if (userStatus != null)
                    {
                        authorize = (bool)userStatus;
                        IsLoggedIn = true;
                    }
                    else
                    {
                        authorize = false;
                    }
                }
            }

            return authorize;
        }
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            if (IsLoggedIn == false)//----- Return To Login page
            {
                filterContext.Result = new HttpUnauthorizedResult();
            }
            else
            {
                filterContext.Result = new RedirectToRouteResult(
                                   new RouteValueDictionary
                                   {
                                       { "action", "Index" },
                                       { "controller", "Payment" },
                                       {"area","" }
                                   });

            }
        }
    }

我如何申请控制器:

 [CustomAuthrizationPayment()]
    public class CorporateRefrencesController : Controller
    {

0 个答案:

没有答案