我怎么可以用我自己的数据库表MVC 4的用户控件(并与角色表布尔字段授权)? [英] How could I use my own database table for MVC 4 user control (and authorizing with boolean fields in role table)?

本文介绍了我怎么可以用我自己的数据库表MVC 4的用户控件(并与角色表布尔字段授权)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一直保持寻找了几天的答案,阅读一些文章已经但是还是相当人困惑。

Hello guys I having been keeping searching for answers for a few days and read couple of posts already but still quite confused.

我使用的是用户表的字段包括姓,名,电子邮件,密码,角色ID和其他的东西像电话号码等。

I am using a user table with fields including First Name, Last Name, Email, Password, RoleID and other stuff like phone numbers etc.

我也有一个角色表,它在字符串和其他一些领域的角色名一现场站立代表布尔类型不同的接入,如AccessToAlterItemInformation这要是这种角色的用户有谁(AccessToAlterItemInformation == true)而将进入项目编辑页面授予。
有几个问题我想请教一下这个话题:

Also I have a "role table" which has a Field standing for "Role Name" in string and few other fields stands for different Accesses of Boolean type such as "AccessToAlterItemInformation" which if a user with such roles who having (AccessToAlterItemInformation == True) will be granted with access to Item Editing page. There are a few questions I want to ask about this topic:


  1. codeS这样的:

[授权(角色=管理员)]

[Authorize(Roles="admin")]

被用来授权对几个职位我看到了,但我想要做的事更像

were used to authorizing on several posts I saw but I want to do something more like

     [Authorize(user.role.AccessToAlterItemInformation == true)] //I know this is not right but something similar
OR:
    if (User.Roles.AccessToAlterItemInformation == True)
       {
            //Do something as Access granted
        }

我怎么能做到这一点? (或者至少达到类似的东西其他一些方法,所以我可以做一个网站授权根据不同的访问)

How could I achieve this? (or some other approaches which at least achieve something similar to that so I can make a website Authorizing according to different accesses)

-2。与上述的第一个问题的要求,我要实现使用已创建的用户表和角色表用剃刀一个MVC 4 Web应用程序的成员/用户系统。我怎么能做到这一点?我想用尽可能的一切已经有(asp.net,simplemembership等),并作出变化不大越好,因为我真的只剩这个项目一点时间。请帮帮我!在此先感谢!

-2. with the requirements as first question described above, I have to implement the member/user system with a MVC 4 Web Application with Razor using already created User Table and Role Table. How could I achieve that? I want to use as much as possible of whatever is already there (asp.net, simplemembership etc.) and make as little changes as possible because I really only have little time left for this project. Please help me! Thanks in advance!

和我的英语不好

推荐答案

您必须定义一个自定义属性授权来做到这一点。

You will have to define a custom Authorize attribute to do this.

[Authorize(user.role.AccessToAlterItemInformation == true)]

应该改成这样的事情。

It should be changed to something like this.

[Authorize(Permissions = Access.EditItemInformation)]

其中,访问是一个标志枚举和权限是自定义一个成员变量(类型访问)授权属性定义的类。

where Access is a Flag enum and Permissions is a member variable (of type Access) in the custom Authorize attribute class you define.

您还必须定义枚举标志本身

you will also have to define the enum flag itself

[Flags]
public enum Access: ulong
{
    CreateItemInformation = 0x00000002,
    EditItemInformation = 0x00000004,
    DeleteItemInformation = 0x00000008,
}

通过使用标志,你将能够给一个以上的标志作为权限

By using flags you will be able to give more than one flag as permissions

[Authorize(Permissions = Access.EditItemInformation || Access.CreateItemInformation)]

重写的AuthorizeCore方法中,你会检查是否允许成员变量都有不同类型的访问标志,如果授权,如果没有虚假记载,返回true。这是你如何检查是否给定的访问标志是在权限变量

within the overridden AuthorizeCore method, you'll check if the permission member variable has different types of Access flags and return true if authorized and false if not. This is how you check if a given Access flag is in the Permission variable

Permissions.HasFlag(Access.EditItemInformation);

这是你如何实现自定义属性授权

This is how you'd implement a custom authorize attribute

<一个href=\"http://stackoverflow.com/questions/13264496/asp-net-mvc-4-custom-authorize-attribute-with-permission-$c$cs-without-roles\">ASP.NET MVC 4自定义授权许可codeS属性(无角色)

标志枚举值应在2的幂请看看这些文章理解的标志。

values of Enum Flags should be in power of 2. Please take a look at these articles to understand flags.

http://www.$c$cproject.com/Articles/13740/The-Beginner-s-Guide-to-Using-Enum-Flags

的http:/ /forums.asp.net/t/1917822.aspx/1?+use+of+Enum+with+flags+in+practicle+

希望帮助

这篇关于我怎么可以用我自己的数据库表MVC 4的用户控件(并与角色表布尔字段授权)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆