【问题标题】:Checking Accessibility of a View in MVC5在 MVC5 中检查视图的可访问性
【发布时间】:2017-12-21 16:38:42
【问题描述】:

在登录一个帐户后的webform中,我们在每个.aspx文件的Page Load函数中使用以下代码来检查页面是否可以访问:

if (Session["User"]!=null)
    {
       Response.Redirect("LoggedInPage.aspx");

    }

在 MVC5 中执行相同的操作我应该怎么做??

【问题讨论】:

    标签: asp.net asp.net-mvc-5


    【解决方案1】:

    现在我想我明白了,您想阻止用户在登录后进入注册页面。

    查看 AuthorizeAttribute。

    1- ASP.Net MVC: Can the AuthorizeAttribute be overriden?

    2- custom authorize

    public class CustomAuthorize : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
    
            var isAuthorized = base.AuthorizeCore(httpContext);
            if (!isAuthorized)
            {
                return false;
            }
    
            if (httpContext.User.Identity.IsAuthenticated && Request.Url.ToString().Contains("Register"))
            {
                return false;
            }
    
        }
    
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                base.HandleUnauthorizedRequest(filterContext);
            }
            else
            {
                filterContext.Result = new RedirectToRouteResult(new
                RouteValueDictionary(new { controller = "Error", action = "AccessDenied" }));
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      登录后应该进行其他操作

      public ActionResult Login() {
          return RedirectToAction("Index");
      }
      
      public ActionResult Index()
      {
        return View();
      }
      

      【讨论】:

      • 我没有要求。登录帐户后,用户不应访问注册视图。我正在尝试检查他的访问权限,并使用 Register View @Issa Saman 中的代码
      • 我有 2 个控制器主页并登录。在 Home Controller 中有一个名为 Register 的操作。在这里,一个人可以在登录帐户之前注册他的帐户,当一个人登录他的帐户时,他将访问 LogIn/LoggedInPage 视图。然后,如果他尝试访问 URL Home/Register,Register View 会将他重定向到 LogIn/LoggedInPage。其实我想要这个。 @伊萨萨曼
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多