MVC3 - Where is UserId and Roles information

走远了吗. 提交于 2019-12-06 11:08:16

Unless i'm misunderstanding the question...

Those models are concerned with the User model.

UserId and Roles are dealing with authentication and authorization.

Most likely, you are using Forms Authentication and the default ASP.NET Membership provider.

If you want to access the UserId of the current logged in user, you need to add it to the forms authentication ticket when you sign the user in, then you can access it via HttpContext.Current.Request.User.Identity.

If your using FormsAuthentication.SetAuthCookie, then Identity.Name will point to whatever you passed to SetAuthCookie - so you could pass the UserId.

But most people (myself included), use nicknames/usernames for the Identity.Name, so you need to create the Forms Authentication ticket manually and add it to the response cookies.

To see if a user is in a role, do this:

HttpContext.Current.User.IsInRole("Administrator")

This is standard ASP.NET stuff, not specific to ASP.NET MVC.

Also, if your using an ORM like Entity Framework or NHibernate or Linq2Sql, don't map the membership tables.

Create your own User table which has a FK pointing to that table, then your User table becomes the model.

The built in membership provider API will use stored procedures behind the scenes to perform the functionality you require.

This is a models for default views - register and change password.

Role can be taken only if roleprovider is configured.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!