2009-06-05 19 views
2

私はASP.NET 2.0 WebFormsのバックグラウンドから来て、それがすばらしいと思っているASP.NET MVCに慣れています。しかし、私は幾分慣れてきました。How to do ASP.NET MVCのリダイレクト

この時、私の問題は、認証と承認のモデルに関係しています:

私は、ユーザーがプライベートにアクセスしようとしたとき

<authorization> 
     <deny users="?"/> 
     <!-- 
     <allow users="*"/> 
    --> 
    </authorization> 

だから、Web.configファイルの認証部を介してフォルダを制限するために使用されます "ページ "はインデックスページにリダイレクトされます。 MVCでこれをどうすればできますか? 私は、セッションデータにユーザーID(またはオブジェクト)を保存するために使用しました...今、私はMVCの方法でそれをどのように格納するかわかりません。

側の注意点として、私のデータモデルは、このようなテーブルがあります。

CREATE TABLE user_perm (
    user INT, 
    feature INT, 
) 

をそして私は、この表の内容に基づいて、特定のコントローラへのアクセスを制限したいと思います。私はそれを達成できますか?

PS:私はこれらの他の質問に気付いていますが、ベータ版を参照していますが、現在のリリース版にまだ適用されているかどうかは不明です。

ありがとうございます。

+0

私の答えを参照してください。アクセスを許可する特定の要件に合わせてRequiresRole属性を変更できる必要があります。 – Ropstah

答えて

3

コントローラの操作で属性フィルタリングを実行する必要があります。 (良い情報については、thisリンクを参照してください。)

コントローラのアクションによって実際の「ページ」が示されます。私が使用してどのような

(カスタム属性...):

Public Class ProjectController 
    Inherits System.Web.Mvc.Controller 

    <Models.Authentication.RequiresAuthentication()> _ 
    Function Edit(ByVal id As Integer) As ActionResult 

    End Function 

    <Models.Authentication.RequiresRole(Role:="Admin")> _ 
    Function Delete(ByVal id As Integer) As ActionResult 

    End Function 
End Class 

と認可属性:

Namespace Models.Authentication 
    Public Class RequiresAuthenticationAttribute : Inherits ActionFilterAttribute 
     Public Overrides Sub OnActionExecuting(ByVal filterContext As System.Web.Mvc.ActionExecutingContext) 
      If Not filterContext.HttpContext.User.Identity.IsAuthenticated Then 
       Dim redirectOnSuccess As String = filterContext.HttpContext.Request.Url.AbsolutePath 
       Dim redirectUrl As String = String.Format("?ReturnUrl={0}", redirectOnSuccess) 
       Dim loginUrl As String = FormsAuthentication.LoginUrl + redirectUrl 

       filterContext.HttpContext.Response.Redirect(loginUrl, True) 
      End If 
     End Sub 
    End Class 

    Public Class RequiresRoleAttribute : Inherits ActionFilterAttribute 
     Private _role As String 

     Public Property Role() As String 
      Get 
       Return Me._role 
      End Get 
      Set(ByVal value As String) 
       Me._role = value 
      End Set 
     End Property 

     Public Overrides Sub OnActionExecuting(ByVal filterContext As System.Web.Mvc.ActionExecutingContext) 
      If Not String.IsNullOrEmpty(Me.Role) Then 
       If Not filterContext.HttpContext.User.Identity.IsAuthenticated Then 
        Dim redirectOnSuccess As String = filterContext.HttpContext.Request.Url.AbsolutePath 
        Dim redirectUrl As String = String.Format("?ReturnUrl={0}", redirectOnSuccess) 
        Dim loginUrl As String = FormsAuthentication.LoginUrl + redirectUrl 

        filterContext.HttpContext.Response.Redirect(loginUrl, True) 
       Else 
        Dim hasAccess As Boolean = filterContext.HttpContext.User.IsInRole(Me.Role) 
        If Not hasAccess Then 
         Throw New UnauthorizedAccessException("You don't have access to this page. Only " & Me.Role & " can view this page.") 
        End If 
       End If 
      Else 
       Throw New InvalidOperationException("No Role Specified") 
      End If 

     End Sub 
    End Class 
End Namespace 
+1

ewww VerBose.net:P –

+1

申し訳ありませんが、私はVBに戻ります:)。私はC#/ Javascriptのコンストラクトが本当に好きで、VB用の行継続マーク_を嫌いです。しかし、VBは完全に私に読める。 intellisense heheのためのHurray – Ropstah

2

が承認属性を使用します。個々のアクションまたはコントローラ全体に配置できます。ここ

[Authorize(Roles="admin")] 

詳細情報:

http://forums.asp.net/p/1428467/3192831.aspx

+0

そしてuser_permテーブルに基づいてそれを行う方法?承認する必要がありますか? –

+0

カスタム属性を作成する必要があります。カスタム属性の例で私の答えを見てください.... – Ropstah

+0

ええ、私は私のものに自分のカスタム属性を使用します - ropstahの例をチェックしてください。 –

1

彼らは道我々はそれを処理しているが、属性を介して行われます。

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

"[Authorize]"は[[Authorize(Roles = "user")]と同等です。特定のロールについては、[Authorize(Roles = "")]を使用します。