2

ASP.Net MVC 5 アプリケーションを作成しています。私の Web サイトには、3 種類のユーザーが存在します。

  • 管理者
  • 一般ユーザー
  • レストラン

これらの各ユーザーには、独自の機能とアクセス権があります。つまり、それぞれのビューは異なるはずです。

通常とレストランの両方のモデルを作成しました。この機能をサポートするために既存の構造を変更する方法を考えていました。

public class User : IUser
{
    public User()
        : this(String.Empty)
    {
    }

    public User(string userName)
    {
        UserName = userName;
        Id = Guid.NewGuid().ToString();
    }

    [Key]
    public string Id { get; set; }

    [Required]
    public string UserName { get; set; }

    [Required]
    public string FirstName { get; set; }

    [Required]
    public string LastName { get; set; }

    public string Phone { get; set; }
    public string MobilePhone { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    public virtual IList<UserAddress> Addresses { get; set; }
}

public class Restaurant
{
    [Key]
    public int ID { get; set; }

    [Required]
    public string Name { get; set; }

    public virtual IList<RestaurantAddress> Addresses { get; set; }

    public virtual IList<RestaurantFood> Menu { get; set; }

    public virtual IList<Review> Reviews { get; set; }

    [DataType(DataType.Url)]
    public string Website { get; set; }

    [DataType(DataType.PhoneNumber)]
    public string Phone { get; set; }

    [DataType(DataType.PhoneNumber)]
    public string Fax { get; set; }

    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    public int Seats { get; set; }

    public double AverageRating { get; set; }
    public double AveragePrice { get; set; }
}
4

3 に答える 3

2

Q を正しく取得できたかどうかわかりませんが、インターネット アプリケーション テンプレートを使用している場合は、ロール管理を使用してアプリのアクセス制御を簡単に管理できます。

まず、webpages_Rolesデータベースのテーブルにいくつかのロールを追加します。

次に、それらのロールにユーザーを追加するだけです。

Role.AddUserToRole("role1");

コンテンツをフィルタリングするには、次の 2 つの作業を行うだけです。

1) [Authorize] 属性を使用して、コントローラー要求を適切なロールにフィルターします。

[Authorize(Roles = "role1, role2, ...")]

2) 関連するユーザーに適切なコンテンツを表示します。最初に現在のユーザーの役割を取得します。

var roles = Roles.GetRolesForUser(User.Identity.Name);

次に、彼/彼女の役割に応じて、彼/彼女のためにコンテンツをレンダリングします:

bool hasRole1 = roles.Contain("role1") | roles.Contain("admin");
// ...
@if (hasRole1)
{
    // Show content for role1 users...
}
于 2013-08-21T09:11:01.627 に答える
1

承認は、ユーザーの役割に基づいて行うことができます。

オーソリゼーションを作成する際、私たちはそれが動的でなければならないことを常に念頭に置いています。新しいユーザー グループには、異なる権限が付与されます。だから私が提案しているのは、データベースに情報を持つことです。

例えば

ユーザーグループ管理者 一般ユーザー レストラン

ロール すべての権限 基本権限 中間権限

これを取得するには、アクション フィルターを使用する必要があります。 http://msdn.microsoft.com/en-us/library/dd410209(v=vs.100).aspx

次に、各ロールに権限を割り当てる必要があります

すべての権限 - addUser、addResturant など (管理目的でフレンドリ名を使用できます。UI に表示できますが、コントローラー名とアクション名を保存する必要があります。 addUser の場合、フレンドリ名は Add User になり、保存します)以下のように

ActionsTable (actionId, friendName, Controller, Action)
1 -Add User - Users - Add 

RolesActionMapTable (roleId, actionID)
1-1

RolesTable (RoleId,Role Name,Desc)
1-AllPrivileage

GroupsTable (GroupId, GroupName)
1-Admin

GroupRoleMap (groupId, roleID)
1-1

authorize 属性を継承してカスタムの Autorize 属性を作成し、それをすべてのメソッドのフィルターとして適用します。オーバーロードされた関数があり、ユーザーがそのアクションへのアクセスを許可されていることを確認できます。したがって、不正アクセスをブロックできます。

編集

ルートデータからコントローラーとアクションを識別できるため、ユーザーID、コントローラー、およびアクションを使用してdbにクエリを実行し、許可されているかどうかを確認したり、ユーザーグループを取得して、これにアクセスする権限が含まれていることを確認したりできます

編集2

public class CustomAuthorizeAttribute: AuthorizeAttribute
{
   protected virtual bool AuthorizeCore(
    HttpContextBase httpContext)
 {
   // 1.Httpcontext can gives you the controller and action
   // 2. retrive the group of user and check the user is allowed to execute this action
   // 3. if allowed, then return true else return false.
   // 4. You can redirect to another page saying you are not allowed to access this action
  }
)
}


//In controller
public class EmployeeController: Controller {

 [CustomAuthorize]
  public Create()
   {
   }

}

お役に立てれば

于 2013-08-17T08:49:51.377 に答える