本文整理汇总了C#中UserManager.FindByEmailAsync方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.FindByEmailAsync方法的具体用法?C# UserManager.FindByEmailAsync怎么用?C# UserManager.FindByEmailAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserManager
的用法示例。
在下文中一共展示了UserManager.FindByEmailAsync方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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 signInManager = new SignInManager<CustomUser, string>(manager,
HttpContext.GetOwinContext().Authentication);
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
{
var result = await signInManager.PasswordSignInAsync(user.UserName, password, true, false);
if (result == SignInStatus.Success)
{
return Content("Hello, " + user.FirstName + " " + user.LastName);
}
//user.FirstName = "Super";
//user.LastName = "Admin";
//await manager.UpdateAsync(user);
}
return Content("Hello, Index");
}
示例3: CreateUserIfNotExist
private static async System.Threading.Tasks.Task CreateUserIfNotExist(UserManager<ApplicationUser> userManager, string email, string password, string role, string loginProvider = null, string providerKey = null)
{
var user = await userManager.FindByEmailAsync(email);
if (user == null)
{
user = new ApplicationUser { UserName = email, Email = email };
var result = await userManager.CreateAsync(user, password);
if (!result.Succeeded)
{
throw new ApplicationException(string.Join("\n", result.Errors.Select(a => a.Description).ToArray()));
}
await userManager.AddToRoleAsync(user, role);
if (loginProvider != null && providerKey != null)
{
await userManager.AddLoginAsync(user, new UserLoginInfo(loginProvider, providerKey, ""));
}
}
}
示例4: AddAppointments
private static void AddAppointments(SMDbContext context, UserManager<ApplicationUser> userManager)
{
var esth = context.Estheticians.Single(x => x.Id == 1);
var services = context.SpaServices.Take(3).ToList();
var clientUser = new ApplicationUser
{
UserName = "[email protected]",
Email = "[email protected]",
FirstName = "Claire",
LastName = "Smith",
PhoneNumber = "5625551212"
};
Task.FromResult(userManager.CreateAsync(clientUser, "client11").Result);
var client = new Client
{
ApplicationUserId = userManager.FindByEmailAsync("[email protected]").Result.Id,
AppointmentRemindersViaText = true,
Appointments = new List<Appointment>()
};
context.Clients.Add(client);
context.SaveChanges();
var appt = new Appointment
{
ClientId = client.Id,
FirstName = clientUser.FirstName,
LastName = clientUser.LastName,
Email = clientUser.Email,
PhoneNumber = clientUser.PhoneNumber,
Services = services,
Esthetician = esth,
StartTime = DateTime.Parse("10/21/2016 17:15"),
LocationId = 1,
EndTime = DateTime.Parse("10/21/2016 18:30"),
RemindViaEmail = client.AppointmentRemindersViaEmail,
RemindViaText = client.AppointmentRemindersViaText,
Gender = Gender.Male
};
context.Appointments.Add(appt);
context.SaveChanges();
}
示例5: CreateUserIfNotExist
private static async Task<ApplicationUser> CreateUserIfNotExist(UserManager<ApplicationUser> userManager, ApplicationUser user, string password, string role, string loginProvider = null, string providerKey = null)
{
//Debugger.Launch();
user.EmailConfirmed = true;
user.Email = user.Email ?? user.UserName;
if (await userManager.FindByEmailAsync(user.Email) == null)
{
var result = await userManager.CreateAsync(user, password);
if (!result.Succeeded)
{
throw new ApplicationException(string.Join("\n", result.Errors.Select(a => a.Description).ToArray()));
}
await userManager.AddToRoleAsync(user, role);
if (loginProvider != null && providerKey != null)
{
await userManager.AddLoginAsync(user, new UserLoginInfo(loginProvider, providerKey, ""));
}
}
return user;
}
示例6: Index
// GET: Home
public async Task<ActionResult> Index()
{
var context = new IdentityDbContext<IdentityUser>(); // DefaultConnection
var store = new UserStore<IdentityUser>(context);
var manager = new UserManager<IdentityUser>(store);
var email = "[email protected]";
var password = "Passw0rd";
var user = await manager.FindByEmailAsync(email);
if (user == null)
{
user = new IdentityUser
{
UserName = email,
Email = email
};
await manager.CreateAsync(user, password);
}
return Content("Hello, Index");
}
示例7: Reset
public async Task<JsonData> Reset(UserViewModel model)
{
try
{
var db = new DataContext();
var userMan = new UserManager<MyUser>(new UserStore<MyUser>(db));
userMan.UserValidator = new UserValidator<MyUser>(userMan)
{
AllowOnlyAlphanumericUserNames =
false
};
var user = await userMan.FindByEmailAsync(model.Email);
if (user == null) throw new Exception("please check the email address");
//todo: generate a unique password and email it to the user
var newPassword = user.FullName.Substring(2, 3) + user.PasswordHash.Substring(0, 5);
var result = await userMan.RemovePasswordAsync(user.Id);
if (!result.Succeeded) throw new Exception(string.Join(", ", result.Errors));
var result2 = await userMan.AddPasswordAsync(user.Id, newPassword);
if (!result2.Succeeded) throw new Exception(string.Join(", ", result2.Errors));
//todo: Email the new password to the user
return DataHelpers.ReturnJsonData(null, true, "A new password has been emailed to your email address");
}
catch (Exception e)
{
return DataHelpers.ExceptionProcessor(e);
}
}
示例8: SetValidateEmail
private void SetValidateEmail(UserManager<IdentityUser> manager,
UserStore<IdentityUser> store,
IdentityUser user,
string strNewEmail)
{
string originalEmail = user.Email;
var taskUserSet = manager.SetEmailAsync(user.Id, strNewEmail);
taskUserSet.Wait();
Assert.IsTrue(taskUserSet.Result.Succeeded, string.Concat(taskUserSet.Result.Errors));
var taskUser = manager.GetEmailAsync(user.Id);
taskUser.Wait();
Assert.AreEqual<string>(strNewEmail, taskUser.Result, "GetEmailAsync: Email not equal");
if (!string.IsNullOrWhiteSpace(strNewEmail))
{
var taskFind = manager.FindByEmailAsync(strNewEmail);
taskFind.Wait();
Assert.AreEqual<string>(strNewEmail, taskFind.Result.Email, "FindByEmailAsync: Email not equal");
}
else
{
//TableQuery query = new TableQuery();
//query.SelectColumns = new List<string>() { "Id" };
//query.FilterString = TableQuery.GenerateFilterCondition("Id", QueryComparisons.Equal, user.Id);
//query.Take(1);
//var results = store.Context.IndexTable.ExecuteQuery(query);
//Assert.IsTrue(results.Where(x=> x.RowKey.StartsWith("E_")).Count() == 0, string.Format("Email index not deleted for user {0}", user.Id));
}
//Should not find old by old email.
if (!string.IsNullOrWhiteSpace(originalEmail))
{
var taskFind = manager.FindByEmailAsync(originalEmail);
taskFind.Wait();
Assert.IsNull(taskFind.Result, "FindByEmailAsync: Old email should not yield a find result.");
}
}
示例9: AddUsersToRoles
private static void AddUsersToRoles(UserManager<ApplicationUser> users, RoleManager<ApplicationRole> roleManager)
{
var admin = users.FindByEmailAsync("[email protected]").Result;
var esthetician = users.FindByEmailAsync("[email protected]").Result;
Task.FromResult(users.AddToRolesAsync(admin, new[] { "Admin", "Esthetician" }).Result);
Task.FromResult(users.AddToRoleAsync(esthetician, "Esthetician").Result);
}
示例10: AddEstheticians
private static void AddEstheticians(UserManager<ApplicationUser> users, RoleManager<ApplicationRole> roles, SMDbContext context)
{
var esthUser = users.FindByEmailAsync("[email protected]").Result;
var allServices = context.SpaServices.ToList();
var esthetician = new Esthetician
{
ApplicationUserId = esthUser.Id,
Services = allServices.OrderBy(x => x.Id).Take(12).ToList(),
Color = "33cc33"
};
context.Estheticians.Add(esthetician);
var nextUser = users.FindByEmailAsync("[email protected]").Result;
var nextEsth = new Esthetician
{
ApplicationUserId = nextUser.Id,
Services = allServices.ToList(),
Color = "cc33ff"
};
context.Estheticians.Add(nextEsth);
context.SaveChanges();
}
示例11: Seed
private async Task Seed(IUnitOfWork uow , UserManager<ApplicationUser> usrMan, RoleManager<IdentityRole> _RoleManager)
{
var email = "[email protected]";
var RoleName = "CustomerAdmin";
var role = await _RoleManager.FindByNameAsync(RoleName);
if (role == null)
{
role = new IdentityRole { Name = RoleName };
await _RoleManager.CreateAsync(role);
}
RoleName = "superadmin";
role = await _RoleManager.FindByNameAsync( RoleName );
if ( role == null)
{
role = new IdentityRole { Name = "SuperAdmin" };
await _RoleManager.CreateAsync(role);
}
var appUser = await usrMan.FindByEmailAsync(email);
if (appUser != null)
{
var result = await usrMan.DeleteAsync(appUser);
}
else
{
appUser = new ApplicationUser() { UserName = email, Email = email };
var result2 = await usrMan.CreateAsync(appUser, "Shafiro2,");
if (result2.Succeeded)
{
await usrMan.AddToRoleAsync(appUser, RoleName );
var hrmUser = new User()
{
UserID = "7310209296",
FirstName = "Gabriel",
LastName = "Tekin",
UserName = "gabtek",
UserCode = "tekgab",
PhoneNumber = "0702385537"
};
if (uow.UserRepository.GetEagerLoad( w => w.UserID.Equals( hrmUser.UserID, StringComparison.OrdinalIgnoreCase )).FirstOrDefault() == null )
{
uow.UserRepository.Insert(hrmUser);
uow.Save();
}
}
else
{
var code = result2.Errors.FirstOrDefault().Code;
}
}
} // method seed