Asp.Net Identity #3
نویسنده: احمد نواصری
تاریخ: ۱۳۹۴/۰۴/۱۷ ۱۹:۵۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity.Owin;
using Users.Infrastructure;
namespace Users.Controllers
{
public class HomeController : Controller
{
private AppUserManager UserManager
{
get { return HttpContext.GetOwinContext().GetUserManager<AppUserManager>(); }
}
// GET: Home
public ActionResult Index()
{
return View(UserManager.Users);
}
} @using Users.Models
@model IEnumerable<AppUser>
@{
ViewBag.Title = "Index";
}
<div class="panel panel-primary">
<div class="panel-heading">
User Accounts
</div>
<table class="table table-striped">
<tr><th>ID</th><th>Name</th><th>Email</th></tr>
@if (!Model.Any())
{
<tr><td colspan="3" class="text-center">No User Accounts</td></tr>
}
else
{
foreach (AppUser user in Model)
{
<tr>
<td>@user.Id</td>
<td>@user.UserName</td>
<td>@user.Email</td>
</tr>
}
}
</table>
</div>
@Html.ActionLink("Create", "CreateUser", null, new { @class = "btn btn-primary" }) namespace Users.Models
{
public class CreateModel
{
[Required]
public string Name { get; set; }
[Required]
public string Email { get; set; }
[Required]
public string Password { get; set; }
}
} public ActionResult CreateUser()
{
return View();
}
[HttpPost]
public async Task<ActionResult> CreateUser(CreateModel model)
{
if (!ModelState.IsValid)
return View(model);
var user = new AppUser { UserName = model.Name, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
return RedirectToAction("Index");
}
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error);
}
return View(model);
} @model Users.ViewModels.CreateModel
@Html.ValidationSummary(false)
@using (Html.BeginForm())
{
<div class="form-group">
<label>Name</label>
@Html.TextBoxFor(x => x.UserName, new { @class = "form-control" })
</div>
<div class="form-group">
<label>Email</label>
@Html.TextBoxFor(x => x.Email, new { @class = "form-control" })
</div>
<div class="form-group">
<label>Password</label>
@Html.PasswordFor(x => x.Password, new { @class = "form-control" })
</div>
<button type="submit" class="btn btn-primary">Create</button>
@Html.ActionLink("Cancel", "Index", null, new { @class = "btn btn-default" })
}
var manager = new AppUserManager(new UserStore<AppUser>(db))
{
PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = true,
RequireUppercase = true
}
}; فقط دوستان توجه داشته باشید که کد بالا را در متد Create از کلاس AppUserManager استفاده کنید.
اعتبار سنجی نام کاربری
برای اعبارسنجی نام کاربری از کلاس UserValidator به صورت زیر استفاده میکنیم:
manager.UserValidator = new UserValidator<AppUser>(manager)
{
AllowOnlyAlphanumericUserNames = true,
RequireUniqueEmail = true
}; کد بالا را نیز در متد Create از کلاس AppUserManager قرار میدهیم.
ublic class AppIdentityDbContext : IdentityDbContext<AppUser>
{
public AppIdentityDbContext()
: base("IdentityDb") { }
// DbSet Definition
public DbSet<Product> Products { get; set; }
static AppIdentityDbContext()
{
Database.SetInitializer<AppIdentityDbContext>(new IdentityDbInit());
}
public static AppIdentityDbContext Create()
{
return new AppIdentityDbContext();
}
} میتونید استفاده کنید. فقط باید به نکات استفاده از چندین Context در EF 6 Code first دقت داشته باشید.
@Html.TextBoxFor(x => x.Name, new { @class = "form-control" }) private AppUserManager UserManager
{
get{ return HttpContext.GetOwinContext().GetUserManager<AppUserManager>(); }
}