MVC3 - UserId 和 Roles 信息在哪里
这是在 MVC3 中为我创建的示例项目的帐户/会员类生成的模型代码。我没有看到 UserId 字段/属性或角色信息。 UserId 和 Roles 信息在哪里?
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; }
}
}
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; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非我误解了这个问题......
这些模型与用户模型有关。
UserId 和 Roles 处理身份验证和授权。
您很可能正在使用表单身份验证和默认的 ASP.NET 成员资格提供程序。
如果要访问当前登录用户的UserId,则需要在用户登录时将其添加到forms身份验证票据中,然后可以通过
HttpContext.Current.Request.User.Identity<访问它/代码>。
如果您使用
FormsAuthentication.SetAuthCookie
,则Identity.Name
将指向您传递给SetAuthCookie
的任何内容 - 这样您就可以传递 UserId。但大多数人(包括我自己)都使用昵称/用户名作为
Identity.Name
,因此您需要手动创建表单身份验证票证并将其添加到响应 cookie 中。要查看用户是否具有某个角色,请执行以下操作:
HttpContext.Current.User.IsInRole("Administrator")
这是标准 ASP.NET 内容,并非特定于 ASP.NET MVC。
另外,如果您使用 Entity Framework、NHibernate 或 Linq2Sql 等 ORM,请不要映射成员资格表。
创建您自己的 User 表,其中有一个指向该表的 FK,然后您的 User 表将成为模型。
内置的会员资格提供程序 API 将在幕后使用存储过程来执行您所需的功能。
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
, thenIdentity.Name
will point to whatever you passed toSetAuthCookie
- 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.
这是默认视图的模型 - 注册和更改密码。
仅当配置了 roleprovider 时才可以获取角色。
This is a models for default views - register and change password.
Role can be taken only if roleprovider is configured.