我怎么可以参考我自己的表与MVC Controller.User财产? [英] How can I reference my own table with the MVC Controller.User property?

查看:125
本文介绍了我怎么可以参考我自己的表与MVC Controller.User财产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用默认的成员资格提供了MVC 2的项目,我可以参考 User.Identity.UserName 按不管它默认创建。我不想这样做 - 想用我自己的表。所以,我实现了System.Web.Security.MembershipProvider类。我唯一​​已经实现,按照很多帖子在网络上和SO,为的ValidateUser(用户名字符串,字符串密码)

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)

我可以很容易地看到这种接受的用户名和密码字符串,访问我的数据库上下文(LINQ到实体,其实dotConnect提供MySQL的),并检查用户名和密码 - 很简单

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.

但是,我怎么能做出Controller.User对象用我自己的表的属性。例如,我的表称为职员。它有STAFFID是(PK),名字,姓氏,等我怎样才能访问当前上下文用户的STAFFID是如下?

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;

我发现这个职位由马特·Wrock,但我真的不能落实User类,具体IsInRole方法的意义。当这个对象构造,并在做的构造函数的参数从何而来?

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?

推荐答案

我不喜欢的成员资格提供程序,所以我尽量避免不惜一切代价与它打交道。如果你不想使用用户从控制器,你可以做以下。

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.

在你的基地控制器,添加一个名为属性的currentUser 你想使用(可能是LINQ的2 SQL类型)。无论何种类型

In your base controller, add a property called CurrentUser of whatever type you want to use (probably Linq 2 Sql type).

重写你的基地控制器上的初始化方法和运行逻辑来获得正确的用户,如果用户进行身份验证。设置的currentUser 来正确的用户。现在,您可以访问在从基本控制器继承任何控制器属性。

Override the Initialize method on your base controller and run the logic to get the proper user if the user is authenticated. Set CurrentUser to the proper user. Now you have access to that property in any controller that inherits from your base controller.

public BaseController : Controller
{
     public MyUserEntity CurrentUser {get; set;}

     protected override void Initialize(RequestContext requestContext)
     {
            CurrentUser = null;
            if (requestContext.HttpContext.User.Identity.IsAuthenticated)
            {
                var userRepository = new UserRepository();
                CurrentUser = userRepository.GetUser(requestContext.HttpContext.User.Identity.Name);
            }

     }

}

和使用它:

public StaffController : BaseController
{
      public ActionResult View()
      {
           var staffRepository = new StaffRepository();
           var staff = staffRepository(CurrentUser.StaffID);
           return View(staff);
      }
}

这篇关于我怎么可以参考我自己的表与MVC Controller.User财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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