如何使用 MVC Controller.User 属性引用我自己的表?
使用 MVC 2 项目的默认成员资格提供程序,我可以根据它默认创建的内容引用 User.Identity.UserName
。我不想这样做——想使用我自己的桌子。因此,我实现了 System.Web.Security.MembershipProvider 类。根据网络上的许多帖子和SO,我实现的唯一一件事是 ValidateUser(string username, string password)
我可以轻松地看到它接受用户名和密码字符串,我访问我的数据库上下文(LINQ to Entities,实际上是 MySQL 的 dotConnect 提供程序),然后检查用户名和密码——很简单。
但是,如何使 Controller.User 对象使用我自己的表的属性。例如,我的表名为 Staff。它有 StaffID (PK)、FirstName、LastName 等。我如何访问当前上下文用户的 StaffID,如下所示?
int id = User.StaffID;
我找到了 Matt Wrock 的这篇文章,但我无法真正理解实现 User 类,特别是 IsInRole 方法。这个对象是什么时候构造的,构造函数参数从哪里来?
With the default membership provider for MVC 2 projects, I can reference User.Identity.UserName
as per whatever it creates by default. I don't want to do this--want to use my own tables. So, I implemented the System.Web.Security.MembershipProvider class. The only thing I've implemented, as per many posts on the web and SO, is ValidateUser(string username, string password)
I can easily see this accepts username and password strings, I access my database context (LINQ to Entities, actually the dotConnect provider for MySQL), and check the username and password--simple enough.
But, how can I make the Controller.User object use the properties of my own table. For example, my table is called Staff. It has StaffID (PK), FirstName, LastName, etc. How can I access the StaffID of the current contextual user as follows?
int id = User.StaffID;
I found this post by Matt Wrock, but I can't really make sense of implementing the User class, specifically the IsInRole method. When is this object constructed, and where do the constructor arguments come from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不喜欢会员提供商,所以我会不惜一切代价避免与它打交道。如果您不想使用控制器中的
User
,您可以执行以下操作。创建一个基本控制器并让所有控制器继承它。
在您的基本控制器中,添加一个名为
CurrentUser
的属性,无论您要使用什么类型(可能是 Linq 2 Sql 类型)。如果用户已通过身份验证,则重写基本控制器上的
Initialize
方法并运行逻辑以获取正确的用户。将CurrentUser
设置为正确的用户。现在,您可以在从基本控制器继承的任何控制器中访问该属性。并使用它:
I don't like the Membership provider so I try to avoid dealing with it at all cost. In case you rather not use the
User
from the controller, you can do the following.Create a base controller and have all your controllers inherit from it.
In your base controller, add a property called
CurrentUser
of whatever type you want to use (probably Linq 2 Sql type).Override the
Initialize
method on your base controller and run the logic to get the proper user if the user is authenticated. SetCurrentUser
to the proper user. Now you have access to that property in any controller that inherits from your base controller.And to use it:
如果你想让它发挥作用,你必须做很多事情。
您需要定义自己的继承自
Next 的类 您需要定义自己的继承于 Next 的类
该类可以具有您想要引用的属性(例如,StaffID)。
您需要连接到实际的身份验证过程并将 GenericPrincipal 后代的实例分配给“User”属性。
然后,每次引用时,您都必须将其强制转换为您的类型才能访问其他属性。
与您迄今为止必须做的工作相比,这需要做更多的工作。
但这是可行的。
You'll have to do quite a bit if you want to get that to work.
you'll need to define your own class that descend from
Next you'll need define your own class that descends from
This class can have the properties you'd like to reference (StaffID for example).
Thne you'll need to hook into the actual authentication process and assign the instance of your GenericPrincipal descendant to the "User" property.
Then each time you reference if you'll have to cast it to be your type in order to get access to the additional properties.
That's quite a bit more work as compared to what you've had to do so far.
But it's doable.