2010-03-08 56 views
1

我有一个ASP.NET MVC网站。 我有很多需要验证的操作。所以,我需要用户重定向到登录页面,并在登录后(如果成功)重定向回来。 所以我现在想要做的是建立一个新的班级,不要。那种看到如下:通过重定向到登录页面,然后返回ASP.NET MVC身份验证

class AuthChecker 
{ 
    public AuthChecker(string authActionName, string authControllerName) 
    { 
     //... 
    } 

    public ActionResult InvokeIfAuthenticated(Func<ActionResult> body, string errorNoAuth, string returnUrl) 
    { 
     if (Request.IsAuthenticated) 
     { 
      return body(); 
     } 
     else 
     { 
      //set returnUrl and return RedirectToAction(authAction); 
     } 
    } 
} 

所以是好,还是有一些出的现成解决方案来管理这种情况? 或者也许有更好的解决方案?

回答

5

您在查找Authorize属性。

例如[从下面的链接]:

[Authorize] 
public ActionResult AuthenticatedUsers() 
{ 
    return View(); 
} 

[Authorize(Roles = "Admin, Super User")] 
public ActionResult AdministratorsOnly() 
{ 
    return View(); 
} 

[Authorize(Users = "Betty, Johnny")] 
public ActionResult SpecificUserOnly() 
{ 
    return View(); 
} 

Restrict Access to an Action Method

+0

它将使用户被重定向到登录后相同的作用? –

相关问题