How can I reference my own table with the MVC Controller.User property?
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?
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);
}
}
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.
精彩评论