如何使用 MVC Controller.User 属性引用我自己的表?

发布于 2024-10-04 10:08:47 字数 600 浏览 1 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有影子陪我不离不弃 2024-10-11 10:08:47

我不喜欢会员提供商,所以我会不惜一切代价避免与它打交道。如果您不想使用控制器中的User,您可以执行以下操作。

创建一个基本控制器并让所有控制器继承它。

在您的基本控制器中,添加一个名为 CurrentUser 的属性,无论您要使用什么类型(可能是 Linq 2 Sql 类型)。

如果用户已通过身份验证,则重写基本控制器上的 Initialize 方法并运行逻辑以获取正确的用户。将 CurrentUser 设置为正确的用户。现在,您可以在从基本控制器继承的任何控制器中访问该属性。

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);
      }
}

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. 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);
            }

     }

}

And to use it:

public StaffController : BaseController
{
      public ActionResult View()
      {
           var staffRepository = new StaffRepository();
           var staff = staffRepository(CurrentUser.StaffID);
           return View(staff);
      }
}
最好是你 2024-10-11 10:08:47

如果你想让它发挥作用,你必须做很多事情。

您需要定义自己的继承自

GenericIdentity

Next 的类 您需要定义自己的继承于 Next 的类

GenericPrincipal

该类可以具有您想要引用的属性(例如,StaffID)。

您需要连接到实际的身份验证过程并将 GenericPrincipal 后代的实例分配给“User”属性。

然后,每次引用时,您都必须将其强制转换为您的类型才能访问其他属性。

((MyPrincipal)User).StaffID

与您迄今为止必须做的工作相比,这需要做更多的工作。

但这是可行的。

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

GenericIdentity

Next you'll need define your own class that descends from

GenericPrincipal

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.

((MyPrincipal)User).StaffID

That's quite a bit more work as compared to what you've had to do so far.

But it's doable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文