如何在ASP.NET标识中编辑用户
我是ASP.NET身份框架的新手,我正在尝试做一些事情。 我想要做的是编辑已注册的用户,然后将用户详细信息更新到数据库…以前,我使用entity framework,然后生成我的控制器视图并自行建模。 但我想更新我的用户详细信息并将用户列表添加到列表中..
我该怎么做这些东西? 我见过角色方法..但我从来不明白,我该怎么办? 没有使用角色..因为我不需要管理员的目的。 只是,我想更新我的用户详细信息。
创建dbcontext对象“context”,您还需要创建一个模型类“UserEdit”,并在其中包含您要编辑的那些字段。
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 EditUser(UserEdit model) { if (!ModelState.IsValid) { return View(model); } var store = new UserStore(new ApplicationDbContext()); var manager = new UserManager (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"); }
//用于删除用户
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 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"); } }
以下是ListUser的视图:
@model IEnumerable @{ ViewBag.Title = "ListUsers"; } @ViewBag.Message
ManageEditors
S.No. Email EmailConfirmed FirstName LastName Mobile @{ int sno = 1; foreach (var item in Model) { @(sno++) @Html.DisplayFor(modelItem => item.Email) @Html.DisplayFor(modelItem => item.EmailConfirmed) @Html.DisplayFor(modelItem => item.FirstName) @Html.DisplayFor(modelItem => item.LastName) @Html.DisplayFor(modelItem => item.Mobile) @Html.ActionLink("Edit", "EditUser", new { email=item.Email}) @Html.ActionLink("Delete", "DeleteUser", new { id = item.Id }) } }
//下面是我的UserEdit模型
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; } }
//下面是我的IdentityModel.cs类,它有ApplicationDbContext类
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 GenerateUserIdentityAsync(UserManager 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 { public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } } }
希望这能帮到你:)
有一个名为UserManager的 asp.net身份类,这个类将帮助用户信息管理,你可以先找到一个用户使用
- FindByIdAsync
- FindByEmailAsync
- FindByUserName
使用用户对象,您可以使用用户配置文件的新信息更新它
然后使用UpdateAsync方法更新数据库中的用户信息。
在获取用户列表时,您可以使用IdentityDbContext类从中获取用户列表。
- 如何在C#中构建搜索引擎
- Asp.net MVC – 如何哈希密码
- 找不到为MVC 5 DropDownListFor添加占位符的方法
- 在ASP.NET MVC POST中绑定IEnumerable的模型?
- 重用WEB API OData控制器的现有ASP.NET-MVC 5 Identity 2授权,以便轻松登录其他应用程序?
- 如何在c#MVC身份validation中获取google plus个人资料图片
- ASP.Net MVC C#Chrome未在编辑模式下显示日期
- 如何在ASP .NET MVC中将对象列表与用户(Account)关联
- 在ASP.Net MVC中使用DropDownList的最佳编程实践