نگاهی به هویت سنجی کاربران در ASP.NET MVC 5
نویسنده: مرتضی دلیل
تاریخ: ۱۳۹۳/۰۳/۲۲ ۱۱:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
Microsoft.AspNet.Identity.Core Microsoft.AspNet.Identity.EntityFrameWork
namespace MyShop.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
} namespace MyShop.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
[Display(Name = "نام انگلیسی")]
public string EnglishName { get; set; }
[Display(Name = "نام سیستمی")]
public string NameInSystem { get; set; }
[Display(Name = "نام فارسی")]
public string PersianName { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "آدرس ایمیل")]
public string Email { get; set; }
}
} namespace MyShop.Models
{
public class AuthorProduct
{
[Key]
public int AuthorProductId { get; set; }
/* public int UserId { get; set; }*/
[Display(Name = "User")]
public string ApplicationUserId { get; set; }
public int ProductID { get; set; }
public virtual Product Product { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
} namespace MyShop.Models
{
[DisplayName("محصول")]
[DisplayPluralName("محصولات")]
public class Product
{
[Key]
public int ProductID { get; set; }
[Display(Name = "گروه محصول")]
[Required(ErrorMessage = "لطفا {0} را وارد کنید")]
public int ProductGroupID { get; set; }
[Display(Name = "مدت زمان")]
public string Duration { get; set; }
[Display(Name = "نام تهیه کننده")]
public string Producer { get; set; }
[Display(Name = "عنوان محصول")]
[Required(ErrorMessage = "لطفا {0} را وارد کنید")]
public string ProductTitle { get; set; }
[StringLength(200)]
[Display(Name = "کلید واژه")]
public string MetaKeyword { get; set; }
[StringLength(200)]
[Display(Name = "توضیح")]
public string MetaDescription { get; set; }
[Display(Name = "شرح محصول")]
[UIHint("RichText")]
[AllowHtml]
public string ProductDescription { get; set; }
[Display(Name = "قیمت محصول")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#,0 ریال}")]
[UIHint("Integer")]
[Required(ErrorMessage = "لطفا {0} را وارد کنید")]
public int ProductPrice { get; set; }
[Display(Name = "تاریخ ثبت محصول")]
public DateTime? RegisterDate { get; set; }
}
} protected override void Seed(MyShop.Models.ApplicationDbContext context)
{
context.Users.AddOrUpdate(u => u.Id,
new ApplicationUser() { Id = "1",EnglishName = "MortezaDalil", PersianName = "مرتضی دلیل", UserDescription = "توضیح در مورد مرتضی", Email = "mm@mm.com", Phone = "2323", Address = "test", NationalCode = "2222222222", ZipCode = "2222222222" },
new ApplicationUser() { Id = "2", EnglishName = "MarhamatZeinali", PersianName = "محسن احمدی", UserDescription = "توضیح در مورد محسن", Email = "mm@mm.com", Phone = "2323", Address = "test", NationalCode = "2222222222", ZipCode = "2222222222" },
new ApplicationUser() { Id = "3", EnglishName = "MahdiMilani", PersianName = "مهدی محمدی", UserDescription = "توضیح در مورد مهدی", Email = "mm@mm.com", Phone = "2323", Address = "test", NationalCode = "2222222222", ZipCode = "2222222222" },
new ApplicationUser() { Id = "4", EnglishName = "Babak", PersianName = "بابک", UserDescription = "کاربر معمولی بدون توضیح", Email = "mm@mm.com", Phone = "2323", Address = "test", NationalCode = "2222222222", ZipCode = "2222222222" }
);
context.AuthorProducts.AddOrUpdate(u => u.AuthorProductId,
new AuthorProduct() { AuthorProductId = 1, ProductID = 1, ApplicationUserId = "2" },
new AuthorProduct() { AuthorProductId = 2, ProductID = 2, ApplicationUserId = "1" },
new AuthorProduct() { AuthorProductId = 3, ProductID = 3, ApplicationUserId = "3" }
); if (!context.Users.Where(u => u.UserName == "Admin").Any())
{
var roleStore = new RoleStore<IdentityRole>(context);
var rolemanager = new RoleManager<IdentityRole>(roleStore);
var userstore = new UserStore<ApplicationUser>(context);
var usermanager = new UserManager<ApplicationUser>(userstore);
var user = new ApplicationUser {UserName = "Admin"};
usermanager.Create(user, "121212");
rolemanager.Create(new IdentityRole {Name = "admin"});
usermanager.AddToRole(user.Id, "admin");
} PM> Install-Package Microsoft.AspNet.Identity.Samples -Pre