如何编辑ASP.NET身份中的用户 [英] How to edit a user in ASP.NET Identity
问题描述
以前,我使用实体框架,然后生成我的控制器视图并自己建模。但我想更新我的用户详细信息,并将用户列表列入列表。
我该如何做这些东西?我看过角色的方法,但我从来不明白,
我该怎么办?没有使用角色。因为,我不需要管理员的目的。只需要更新我的用户详细信息。
创建一个dbcontext对象上下文,您还需要创建一个模型类UserEdit,并包含您要编辑的那些字段。
private ApplicationDbContext context = new ApplicationDbContext();
//查看用户列表
public ActionResult ListUsers()
{
return View(context.Users.ToList());
}
public ActionResult EditUser(string email)
{
ApplicationUser appUser = new ApplicationUser();
appUser = UserManager.FindByEmail(email);
UserEdit user = new UserEdit();
user.Address = appUser.Address;
user.FirstName = appUser.FirstName;
user.LastName = appUser.LastName;
user.EmailConfirmed = appUser.EmailConfirmed;
user.Mobile = appUser.Mobile;
user.City = appUser.City;
return View(user);
}
[HttpPost]
public async任务< ActionResult> EditUser(UserEdit model)
{
if(!ModelState.IsValid)
{
return View(model);
}
var store = new UserStore< ApplicationUser>(new ApplicationDbContext());
var manager = new UserManager< ApplicationUser>(store);
var currentUser = manager.FindByEmail(model.Email);
currentUser.FirstName = model.FirstName;
currentUser.LastName = model.LastName;
currentUser.Mobile = model.Mobile;
currentUser.Address = model.Address;
currentUser.City = model.City;
currentUser.EmailConfirmed = model.EmailConfirmed;
await manager.UpdateAsync(currentUser);
var ctx = store.Context;
ctx.SaveChanges();
TempData [msg] =配置文件更改已保存!;
return RedirectToAction(ListUser);
}
//删除用户
public ActionResult DeleteUser(string id)
{
if(id == null)
{
return new HttpStatusCodeResult HttpStatusCode.BadRequest);
}
var user = context.Users.Find(id);
if(user == null)
{
return HttpNotFound();
}
return View(context.Users.Find(id));
}
public async任务< ActionResult> UserDeleteConfirmed(string id)
{
var user = await UserManager.FindByIdAsync(id);
var result = await UserManager.DeleteAsync(user);
if(result.Succeeded)
{
TempData [UserDeleted] =用户已成功删除;
return RedirectToAction(ManageEditors);
}
else
{
TempData [UserDeleted] =删除用户错误;
return RedirectToAction(ManageEditors);
}
}
以下是ListUser的查看器:
@model IEnumerable< SampleApp.Models.ApplicationUser> ;
@ {
ViewBag.Title =ListUsers;
}
< div class =row>
< div class =col-md-12>
< div>
< h3> @ ViewBag.Message< / h3>
< / div>
< div>
< h2> ManageEditors< / h2>
< table class =table>
< tr>
< th>
S.No.
< / th>
< th>
电子邮件
< / th>
< th>
EmailConfirmed
< / th>
< th>
FirstName
< / th>
< th>
姓氏
< / th>
< th>
手机
< / th>
< th>< / th>
< / tr>
@ {int sno = 1;
foreach(模型中的var item)
{
< tr>
< td>
@(sno ++)
< / td>
< td>
@ Html.DisplayFor(modelItem => item.Email)
< / td>
< td>
@ Html.DisplayFor(modelItem => item.EmailConfirmed)
< / td>
< td>
@ Html.DisplayFor(modelItem => item.FirstName)
< / td>
< td>
@ Html.DisplayFor(modelItem => item.LastName)
< / td>
< td>
@ Html.DisplayFor(modelItem => item.Mobile)
< / td>
< td>
@ Html.ActionLink(Edit,EditUser,new {email = item.Email})
@ Html.ActionLink(Delete,DeleteUser,new {id = item.Id })
< / td>
< / tr>
}
}
< / table>
< / div>
< / div>
< / div>
//以下是我的UserEdit模型
public class UserEdit
{
[Display(Name =电子邮件)]
public string Email {get;组;
[必需]
[Display(Name =First Name)]
public string FirstName {get;组; }
[必需]
[Display(Name =Last Name)]
public string LastName {get;组; }
[Display(Name =Mobile)]
public string Mobile {get;组; }
[Display(Name =Address)]
public string Address {get;组; }
[Display(Name =City)]
public string City {get;组; }
public bool EmailConfirmed {get;组; }
}
//以下是我的IdentityModel.cs类,它们有ApplicationDbContext类
使用System.Data.Entity;
使用System.Security.Claims;
使用System.Threading.Tasks;
使用Microsoft.AspNet.Identity;
使用Microsoft.AspNet.Identity.EntityFramework;
命名空间SampleApp.Models
{
//您可以通过向ApplicationUser类添加更多属性来添加用户的配置文件数据,请访问http://go.microsoft。 com / fwlink /?LinkID = 317594了解更多。
public class ApplicationUser:IdentityUser
{
public async Task< ClaimsIdentity> GenerateUserIdentityAsync(UserManager< ApplicationUser> manager)
{
//请注意,authenticationType必须与CookieAuthenticationOptions.AuthenticationType
中定义的那个匹配。var userIdentity = await manager.CreateIdentityAsync(this,DefaultAuthenticationTypes.ApplicationCookie);
//在此添加自定义用户声明
return userIdentity;
}
//实体框架添加到自动生成的表的第一个方法(ASPNETUSERS)
public string FirstName {get;组; }
public string LastName {get;组; }
public string DOB {get;组; }
public string Sex {get;组; }
public string Address {get;组; }
public string City {get;组; }
public string Mobile {get;组; }
}
public class ApplicationDbContext:IdentityDbContext< ApplicationUser>
{
public ApplicationDbContext()
:base(DefaultConnection,throwIfV1Schema:false)
{
}
public static ApplicationDbContext Create ()
{
return new ApplicationDbContext();
}
}
}
希望这个帮助你:)
I am new to the ASP.NET Identity framework and I am trying to do some things. What I want to do is to edit the user who has already register and then update the user details to the database... Previously, I use entity framework, and then its generated my controller view and model it self. But I want to update my user details and getting a list of users into a list..
How do I do these stuff? I have seen role methods..but I never understand, How can I do? without using role..Because, I don't need administrator purposes. Only, I want to update my user details.
Create a dbcontext object "context" and you also need to create a model class "UserEdit" and include those fields in it which you wants to edit.
private ApplicationDbContext context = new ApplicationDbContext();
// To view the List of User
public ActionResult ListUsers ()
{
return View(context.Users.ToList());
}
public ActionResult EditUser(string email)
{
ApplicationUser appUser = new ApplicationUser();
appUser = UserManager.FindByEmail(email);
UserEdit user = new UserEdit();
user.Address = appUser.Address;
user.FirstName = appUser.FirstName;
user.LastName = appUser.LastName;
user.EmailConfirmed = appUser.EmailConfirmed;
user.Mobile = appUser.Mobile;
user.City = appUser.City;
return View(user);
}
[HttpPost]
public async Task<ActionResult> EditUser(UserEdit model)
{
if (!ModelState.IsValid)
{
return View(model);
}
var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
var manager = new UserManager<ApplicationUser>(store);
var currentUser = manager.FindByEmail(model.Email);
currentUser.FirstName = model.FirstName;
currentUser.LastName = model.LastName;
currentUser.Mobile = model.Mobile;
currentUser.Address = model.Address;
currentUser.City = model.City;
currentUser.EmailConfirmed = model.EmailConfirmed;
await manager.UpdateAsync(currentUser);
var ctx = store.Context;
ctx.SaveChanges();
TempData["msg"] = "Profile Changes Saved !";
return RedirectToAction("ListUser");
}
// for deleting a user
public ActionResult DeleteUser(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var user = context.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(context.Users.Find(id));
}
public async Task<ActionResult> UserDeleteConfirmed(string id)
{
var user = await UserManager.FindByIdAsync(id);
var result = await UserManager.DeleteAsync(user);
if (result.Succeeded)
{
TempData["UserDeleted"] = "User Successfully Deleted";
return RedirectToAction("ManageEditors");
}
else
{
TempData["UserDeleted"] = "Error Deleting User";
return RedirectToAction("ManageEditors");
}
}
Below is the View for ListUser:
@model IEnumerable<SampleApp.Models.ApplicationUser>
@{
ViewBag.Title = "ListUsers";
}
<div class="row">
<div class="col-md-12">
<div>
<h3>@ViewBag.Message</h3>
</div>
<div>
<h2>ManageEditors</h2>
<table class="table">
<tr>
<th>
S.No.
</th>
<th>
Email
</th>
<th>
EmailConfirmed
</th>
<th>
FirstName
</th>
<th>
LastName
</th>
<th>
Mobile
</th>
<th></th>
</tr>
@{ int sno = 1;
foreach (var item in Model)
{
<tr>
<td>
@(sno++)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmailConfirmed)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Mobile)
</td>
<td>
@Html.ActionLink("Edit", "EditUser", new { email=item.Email})
@Html.ActionLink("Delete", "DeleteUser", new { id = item.Id })
</td>
</tr>
}
}
</table>
</div>
</div>
</div>
// below is my UserEdit Model
public class UserEdit
{
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Mobile")]
public string Mobile { get; set; }
[Display(Name = "Address")]
public string Address { get; set; }
[Display(Name = "City")]
public string City { get; set; }
public bool EmailConfirmed { get; set; }
}
//below is my IdentityModel.cs class which have ApplicationDbContext class
using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
namespace SampleApp.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 async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
//Extra column added to auto generated Table by Code First approach (ASPNETUSERS) by Entity Framework
public string FirstName { get; set; }
public string LastName { get; set; }
public string DOB { get; set; }
public string Sex { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Mobile { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
Hope this help you :)
这篇关于如何编辑ASP.NET身份中的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!