本文整理汇总了C#中UserManager.UpdateAsync方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.UpdateAsync方法的具体用法?C# UserManager.UpdateAsync怎么用?C# UserManager.UpdateAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserManager
的用法示例。
在下文中一共展示了UserManager.UpdateAsync方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Index
// GET: Home
public async Task<ActionResult> Index()
{
var context = new ApplicationDbContext(); // DefaultConnection
var store = new UserStore<CustomUser>(context);
var manager = new UserManager<CustomUser>(store);
var email = "[email protected]";
var password = "Passw0rd";
var user = await manager.FindByEmailAsync(email);
if (user == null)
{
user = new CustomUser
{
UserName = email,
Email = email,
FirstName = "Super",
LastName = "Admin"
};
await manager.CreateAsync(user, password);
}
else
{
user.FirstName = "Super";
user.LastName = "Admin";
await manager.UpdateAsync(user);
}
return Content("Hello, Index");
}
示例2: UpdateUser
public async Task<IdentityResult> UpdateUser(IdentityUser user)
{
IdentityResult user1 = null;
try
{
_ctx = new AuthContext();
_userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(_ctx));
user1 = await _userManager.UpdateAsync(user);
}
catch (Exception ex) { }
return user1;
}
示例3: Save
public async Task<WikiDownUser> Save(IPrincipal principal, UserManager<WikiDownUser> userManager)
{
var user = await userManager.FindByNameAsync(this.UserName);
var roles = this.GetRoles(principal, user);
if (user != null)
{
if (user.UserName == principal.Identity.Name)
{
var userAccessLevel = ArticleAccessHelper.GetAccessLevel(user.Roles);
if (userAccessLevel < ArticleAccessLevel.Admin)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
}
user.SetRoles(roles);
user.SetDisplayName(this.DisplayName);
user.SetEmail(this.Email);
if (!string.IsNullOrWhiteSpace(this.Password))
{
await userManager.RemovePasswordAsync(user.Id);
await userManager.AddPasswordAsync(user.Id, this.Password);
}
await userManager.UpdateAsync(user);
WikiDownUserCacheHelper.Clear(user.UserName);
}
else
{
user = new WikiDownUser(this.UserName) { Roles = roles };
user.SetDisplayName(this.DisplayName);
user.SetEmail(this.Email);
await userManager.CreateAsync(user, this.Password);
}
return user;
}
示例4: UpdateUser
public async Task<ActionResult> UpdateUser(string FirstName, string LastName, HttpPostedFileBase file)
{
var userId = User.Identity.GetUserId();
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var currentUser = manager.FindById(User.Identity.GetUserId());
if (file != null)
{
using (MemoryStream ms = new MemoryStream())
{
file.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
currentUser.Cover = array;
currentUser.CoverType = file.ContentType;
}
}
currentUser.LastName = LastName;
currentUser.FirstName = FirstName;
await manager.UpdateAsync(currentUser);
return View("Index");
}
示例5: DisableLockout
void DisableLockout(UserManager<IdentityUser> manager, IdentityUser identityUser)
{
if (identityUser != null)
{
identityUser.LockoutEnabled = true;
identityUser.LockoutEndDateUtc = null;
manager.UpdateAsync(identityUser);
}
}
示例6: LockoutUntilYear3015
void LockoutUntilYear3015(UserManager<IdentityUser> manager, IdentityUser identityUser)
{
if (identityUser != null)
{
identityUser.LockoutEnabled = true;
identityUser.LockoutEndDateUtc = DateTime.UtcNow.AddYears(999);
manager.UpdateAsync(identityUser);
}
}
示例7: Create
public async Task<ActionResult> Create()
{
FitBit fitbit = new FitBit();
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
try
{
var currentUser = userManager.FindById(User.Identity.GetUserId());
var progress = Convert.ToDouble(currentUser.FitBitProgress);
var goal = Convert.ToDouble(currentUser.FitBitGoal);
var dateStart = Convert.ToDateTime(currentUser.dateStartFitBit.ToString());
var dateEnd = Convert.ToDateTime(currentUser.dateEndFitBit.ToString());
var percentEarned = (progress / goal) * 100;
int percentEarnedShort = Convert.ToInt32(percentEarned);
dateStart.ToShortDateString();
dateEnd.ToShortDateString();
fitbit.User = currentUser.UserName;
fitbit.Progress = progress.ToString();
fitbit.Goal = goal.ToString();
fitbit.dateStart = dateStart;
fitbit.dateEnd = dateEnd;
fitbit.percentageEarned = percentEarnedShort.ToString();
db.FitBits.Add(fitbit);
db.SaveChanges();
TempData["ValidationMessage"] = "Stats Submitted for " + " " + dateStart + " - " + dateEnd;
var fitBitProgressNew = 0;
currentUser.FitBitProgress = fitBitProgressNew;
try
{
await userManager.UpdateAsync(currentUser);
var saveUser = userStore.Context;
await saveUser.SaveChangesAsync();
}
catch
{
TempData["ValidationMessage"] = "Error: Stats Not Submitted!";
return RedirectToAction("Index", "Fitbit");
}
return RedirectToAction("Manage", "Fitbit");
}
catch
{
TempData["ValidationMessage"] = "Error: Stats Not Submitted!";
return RedirectToAction("Index", "Fitbit");
}
}
示例8: AddPhoneNumber
public async Task<ActionResult> AddPhoneNumber(AddPhoneNumberViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
ApplicationDbContext db = new ApplicationDbContext();
var userStore = new UserStore<ApplicationUser>(db);
var userManager = new UserManager<ApplicationUser>(userStore);
var user = await userManager.FindByIdAsync(User.Identity.GetUserId());
if (user != null)
{
user.PhoneNumber = model.Number;
}
var result = await userManager.UpdateAsync(user);
var ctx = userStore.Context;
var result1 = await ctx.SaveChangesAsync();
// However, it always succeeds inspite of not updating the database
if (!result.Succeeded)
{
AddErrors(result);
}
return RedirectToAction("Index", "Manage");
}
示例9: updateDateFitBit
public async Task<ActionResult> updateDateFitBit(DateTime dateStart)
{
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
var currentUser = userManager.FindById(User.Identity.GetUserId());
var dateStartUpdate = dateStart.Date;
//dateEnd is automatically 7 days after date start
var dateEndUpdate = dateStartUpdate.Date.AddDays(7);
currentUser.dateStartFitBit = dateStartUpdate.ToShortDateString();
currentUser.dateEndFitBit = dateEndUpdate.ToShortDateString();
try
{
await userManager.UpdateAsync(currentUser);
var saveUser = userStore.Context;
await saveUser.SaveChangesAsync();
TempData["ValidationMessage"] = "Date Start Set To: " + " " + dateStartUpdate.ToShortDateString();
}
catch
{
TempData["ValidationMessage"] = "Error: Date Start Not Set!";
}
return RedirectToAction("Index");
}
示例10: updateGoalFitBit
public async Task<ActionResult> updateGoalFitBit(string updateGoal)
{
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
var currentUser = userManager.FindById(User.Identity.GetUserId());
var fitBitUpdateGoal = Convert.ToInt32(updateGoal);
currentUser.FitBitGoal = fitBitUpdateGoal;
try
{
await userManager.UpdateAsync(currentUser);
var saveUser = userStore.Context;
await saveUser.SaveChangesAsync();
TempData["ValidationMessage"] = "Goal Updated To: " + " " + fitBitUpdateGoal;
}
catch
{
TempData["ValidationMessage"] = "Error: Goal Not Updated!";
}
return RedirectToAction("Index");
}
示例11: addProgressFitBit
public async Task<ActionResult> addProgressFitBit(string addProgress)
{
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
var currentUser = userManager.FindById(User.Identity.GetUserId());
var fitBitProgressOriginal = currentUser.FitBitProgress;
var fitBitAdd = Convert.ToInt32(addProgress);
var fitBitProgressNew = (fitBitProgressOriginal += fitBitAdd);
currentUser.FitBitProgress = fitBitProgressNew;
try
{
await userManager.UpdateAsync(currentUser);
var saveUser = userStore.Context;
await saveUser.SaveChangesAsync();
TempData["ValidationMessage"] = "Progress Added!";
}
catch
{
TempData["ValidationMessage"] = "Error: Progress Not Added!";
}
return RedirectToAction("Index");
}
示例12: ConfigureUsers
public static async void ConfigureUsers(IEnumerable<InMemoryUser> users, EntityFrameworkServiceOptions options, UserManager userManager) {
using(var db = new Contexto(options.ConnectionString)) {
if(!db.Users.Any()) {
foreach(var u in users) {
var entity = new Usuario { Email = u.Claims.First(x=>x.Type==Constants.ClaimTypes.Email).Value , UserName = u.Username };
var response = await userManager.CreateAsync(entity, u.Password);
if(!response.Succeeded) {
throw new Exception("Não foi possível criar o usuario" + u.Username + response.Errors.ToString());
}
else {
var user = await userManager.FindAsync(u.Username,u.Password);
foreach(var c in u.Claims) {
await userManager.AddClaimAsync(user.Id,c);
}
await userManager.UpdateAsync(user);
}
}
db.SaveChanges();
}
}
}
示例13: Edit
public async Task<ActionResult> Edit(EditViewModel model)
{
if (ModelState.IsValid)
{
var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
var manager = new UserManager<ApplicationUser>(store);
// Get the current application user
var user = UserManager.FindById(User.Identity.GetUserId());;
// Update the details
user.FirstName = model.FirstName;
user.LastName = model.LastName;
user.EMail = model.EMail;
user.City = model.City;
user.Age = model.Age;
// This is the part that doesn't work
var result = await manager.UpdateAsync(user);
var ctx = store.Context;
ctx.SaveChanges();
// However, it always succeeds inspite of not updating the database
if (!result.Succeeded)
{
AddErrors(result);
}
}
return RedirectToAction("Manage");
}