2010-03-08 33 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

它將使用戶被重定向到登錄後相同的作用? –

相關問題