MVC3 - 哪里是用户ID和角色信息 [英] MVC3 - Where is UserId and Roles information

查看:186
本文介绍了MVC3 - 哪里是用户ID和角色信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在MVC3生成帐户/会员类我创建了一个示例项目示范code。我没有看到用户ID字段/属性或角色的信息..
哪里是用户ID和角色的信息?

This is the Model code generated in MVC3 for Account/Membership class for a sample project I created. I do not see UserId field/attribute or Roles information.. Where is the UserId and Roles information ?

  public class ChangePasswordModel
{
    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Current password")]
    public string OldPassword { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "New password")]
    public string NewPassword { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm new password")]
    [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

public class LogOnModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

public class RegisterModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

}

推荐答案

除非我误解的问题...

Unless i'm misunderstanding the question...

这些模型所关注的用户的模式

Those models are concerned with the User model.

用户ID和角色正在处理的验证和授权

UserId and Roles are dealing with authentication and authorization.

最有可能的,你正在使用窗体身份验证和默认的ASP.NET成员资格人员。

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

如果您要访问登录的用户当前的用户ID,你需要它,当你登录的用户添加到窗体身份验证票,那么你可以通过 HttpContext.Current访问它。 Request.User.Identity

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.

如果您使用 FormsAuthentication.SetAuthCookie ,那么 Identity.Name 将指向任何你传递给 SetAuthCookie - 所以你可以通过用户ID

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

但大多数人(包括我自己),使用昵称/用户名的 Identity.Name ,所以你需要手动创建窗体身份验证票,并将其添加到响应饼干。

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(管理员)

这是标准的ASP.NET的东西,而不是具体到ASP.NET MVC。

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

另外,如果您使用类似实体框架或NHibernate的或LINQ2SQL一个ORM,不映射表成员

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

创建具有FK指向该表自己的用户表,那么你的用户表变为模型。

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

内置的成员资格提供API将使用后台存储过程来执行所需的功能。

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

这篇关于MVC3 - 哪里是用户ID和角色信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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