ساخت یک اپلیکیشن ساده ToDo با ASP.NET Identity
نویسنده: آرمین ضیاء
تاریخ: ۱۳۹۲/۱۰/۱۹ ۱۳:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
Database.SetInitializer<MyDbContext>(new MyDbInitializer());
public class MyDbInitializer : DropCreateDatabaseAlways<MyDbContext>
{
protected override void Seed(MyDbContext context)
{
var UserManager = new UserManager<MyUser>(new
UserStore<MyUser>(context));
var RoleManager = new RoleManager<IdentityRole>(new
RoleStore<IdentityRole>(context));
string name = "Admin";
string password = "123456";
//Create Role Admin if it does not exist
if (!RoleManager.RoleExists(name))
{
var roleresult = RoleManager.Create(new IdentityRole(name));
}
//Create User=Admin with password=123456
var user = new MyUser();
user.UserName = name;
var adminresult = UserManager.Create(user, password);
//Add User Admin to Role Admin
if (adminresult.Succeeded)
{
var result = UserManager.AddToRole(user.Id, name);
}
base.Seed(context);
}
} public class MyUser : IdentityUser
{
public string HomeTown { get; set; }
public virtual ICollection<ToDo>
ToDoes { get; set; }
}
public class ToDo
{
public int Id { get; set; }
public string Description { get; set; }
public bool IsDone { get; set; }
public virtual MyUser User { get; set; }
} public class MyDbContext : IdentityDbContext<MyUser>
{
public MyDbContext()
: base("DefaultConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
public System.Data.Entity.DbSet<AspnetIdentitySample.Models.ToDo>
ToDoes { get; set; }
} [Authorize] public class ToDoController : Controller
private MyDbContext db;
private UserManager<MyUser> manager;
public ToDoController()
{
db = new MyDbContext();
manager = new UserManager<MyUser>(new UserStore<MyUser>(db));
} public async Task<ActionResult> Create
([Bind(Include="Id,Description,IsDone")] ToDo todo)
{
var currentUser = await manager.FindByIdAsync
(User.Identity.GetUserId());
if (ModelState.IsValid)
{
todo.User = currentUser;
db.ToDoes.Add(todo);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(todo);
} public ActionResult Index()
{
var currentUser = manager.FindById(User.Identity.GetUserId());
return View(db.ToDoes.ToList().Where(
todo => todo.User.Id == currentUser.Id));
} [Authorize(Roles="Admin")]
public async Task<ActionResult> All()
{
return View(await db.ToDoes.ToListAsync());
} @model IEnumerable<AspnetIdentitySample.Models.ToDo>
@{
ViewBag.Title = "Index";
}
<h2>List of ToDoes for all Users</h2>
<p>
Notice that we can see the User info (UserName) and profile info such as HomeTown for the user as well.
This was possible because we associated the User object with a ToDo object and hence
we can get this rich behavior.
12: </p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.IsDone)
</th>
<th>@Html.DisplayNameFor(model => model.User.UserName)</th>
<th>@Html.DisplayNameFor(model => model.User.HomeTown)</th>
</tr>
25:
26: @foreach (var item in Model)
27: {
28: <tr>
29: <td>
30: @Html.DisplayFor(modelItem => item.Description)
31: </td>
32: <td>
@Html.DisplayFor(modelItem => item.IsDone)
</td>
<td>
@Html.DisplayFor(modelItem => item.User.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.User.HomeTown)
</td>
</tr>
}
</table> <li>@Html.ActionLink("ToDo", "Index", "ToDo")</li>
<li>@Html.ActionLink("ToDo for User In Role Admin", "All", "ToDo")</li>
پس از ساختن یک ToDo میتوانید لیست رکوردهای خود را مشاهده کنید. دقت داشته باشید که رکوردهایی که کاربران دیگر ثبت کرده اند برای شما نمایش داده نخواهند شد.
User = Admin Password = 123456
var currentUser = await manager.FindByIdAsync(User.Identity.GetUserId());
مطلب و پروژه اعمال تزریق وابستگیها به مثال رسمی ASP.NET Identity رو مطالعه کنید.