C# 如何使用身份提供程序实现对asp.net mvc 4应用程序的身份验证
我有一个ASP.NET MVC 4 Web应用程序APP1,它使用DB First方法实现了实体框架。我现在想在其中添加身份验证,包括电子邮件和角色,如果我做到了这一点,最先进的方法就是使用身份提供程序。我尝试的是制作一个新的mvc 4 web应用程序APP2,并将自动生成的帐户模型,-views and-controller添加到我现有的APP1应用程序中,并使用附加的EmailAddress属性扩展AccountModel:C# 如何使用身份提供程序实现对asp.net mvc 4应用程序的身份验证,c#,.net,asp.net-mvc,asp.net-mvc-4,asp.net-identity,C#,.net,Asp.net Mvc,Asp.net Mvc 4,Asp.net Identity,我有一个ASP.NET MVC 4 Web应用程序APP1,它使用DB First方法实现了实体框架。我现在想在其中添加身份验证,包括电子邮件和角色,如果我做到了这一点,最先进的方法就是使用身份提供程序。我尝试的是制作一个新的mvc 4 web应用程序APP2,并将自动生成的帐户模型,-views and-controller添加到我现有的APP1应用程序中,并使用附加的EmailAddress属性扩展AccountModel: [Table("UserProfile")] public cla
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string EmailAddress { get; set; }
public string UserName { get; set; }
}
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[Display(Name = "Email")]
public string EmailAddress { 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; }
}
我还将电子邮件地址的标签和文本框添加到Register.cshtml视图中:
<li>
@Html.LabelFor(m => m.EmailAddress)
@Html.TextBoxFor(m => m.EmailAddress)
</li>
到目前为止,还不错,但是否也可以使用我的EF SQL Server实例来代替呢?还有,扮演角色最简单的方法是什么
更新2
我可以通过更改web.config文件中的DefaultConnectionString来更改DB
谢谢发布您的Identity framework数据上下文的代码,该数据上下文继承自Identity YDBContext类,您是否在使用db first方法时绑定了模型?不确定我是否理解正确,但我认为它们是绑定的,因为模型是由EF生成的。你需要全部代码吗?它有800行..我知道你说的800行是什么意思,但它是整个数据库的数据上下文,我假设类的一部分是从DbContext继承的,不是吗?我指的是identity framework数据库表的部分,它应该从IdentityDbContext继承,而不是DbContext。这是否回答了您的问题?我想您最好看看VisualStudio附带的模板。
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-APP1-20131105213440;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-APP1-20131105213440.mdf" providerName="System.Data.SqlClient" />