MVC3 - Where is UserId and Roles information

2019-06-01 18:14发布

问题:

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

}

回答1:

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.



回答2:

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

Role can be taken only if roleprovider is configured.