本文整理汇总了C#中UserManager.FindByEmail方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.FindByEmail方法的具体用法?C# UserManager.FindByEmail怎么用?C# UserManager.FindByEmail使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserManager
的用法示例。
在下文中一共展示了UserManager.FindByEmail方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddUserAndRole
internal void AddUserAndRole()
{
ApplicationDbContext context = new ApplicationDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
RoleStore<IdentityRole> roleStore = new RoleStore<IdentityRole>(context);
RoleManager<IdentityRole> roleMgr = new RoleManager<IdentityRole>(roleStore);
//create admin role
if(!roleMgr.RoleExists("admin")) {
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "admin" });
}
//create master user
UserManager<ApplicationUser> userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
ApplicationUser appUser = new ApplicationUser {
UserName = "Adam",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "Baseball1!");
//add to admin role
if(!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "admin")) {
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "admin");
}
}
示例2: InitializeRoles
/// <summary>
/// Checks for the three roles - Admin, Employee and Complainant and
/// creates them if not present
/// </summary>
public static void InitializeRoles()
{ // Access the application context and create result variables.
ApplicationDbContext context = new ApplicationDbContext();
IdentityResult IdUserResult;
// Create a RoleStore object by using the ApplicationDbContext object.
// The RoleStore is only allowed to contain IdentityRole objects.
var roleStore = new RoleStore<IdentityRole>(context);
RoleManager roleMgr = new RoleManager();
if (!roleMgr.RoleExists("Administrator"))
{
roleMgr.Create(new ApplicationRole { Name = "Administrator" });
}
if (!roleMgr.RoleExists("Employee"))
{
roleMgr.Create(new ApplicationRole { Name = "Employee" });
}
if (!roleMgr.RoleExists("Complainant"))
{
roleMgr.Create(new ApplicationRole { Name = "Complainant" });
}
if (!roleMgr.RoleExists("Auditor"))
{
roleMgr.Create(new ApplicationRole { Name = "Auditor" });
}
// Create a UserManager object based on the UserStore object and the ApplicationDbContext
// object. Note that you can create new objects and use them as parameters in
// a single line of code, rather than using multiple lines of code, as you did
// for the RoleManager object.
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var appUser = new ApplicationUser
{
UserName = "Administrator",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "Admin123");
// If the new "canEdit" user was successfully created,
// add the "canEdit" user to the "canEdit" role.
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "Administrator"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "Administrator");
}
appUser = new ApplicationUser
{
UserName = "Auditor",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "Auditor123");
// If the new "canEdit" user was successfully created,
// add the "canEdit" user to the "canEdit" role.
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "Auditor"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "Auditor");
}
}
示例3: AddUserAndRole
internal void AddUserAndRole()
{
Models.ApplicationDbContext db = new ApplicationDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
var roleStore = new RoleStore<IdentityRole>(db);
var roleMgr = new RoleManager<IdentityRole>(roleStore);
if (!roleMgr.RoleExists("canEdit"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "canEdit" });
}
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
var appUser = new ApplicationUser
{
UserName = "[email protected]",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "NhatSinh123*");
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "canEdit"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "canEdit");
}
}
示例4: AddUserAndRole
internal void AddUserAndRole()
{
// Access the application context and create result variable.
Models.ApplicationDbContext context = new ApplicationDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
// Create a RoleStore object by using the ApplicationDbContext object.
// The RoleStore is only allowed to contain IdentityRole Objects.
var roleStore = new RoleStore<IdentityRole>(context);
// Create a RoleManager object that is only allowed to contain IdentityRole objects.
// When creating the RoleManager object, you pass in (as a parameter) a new RoleStore object.
var roleMgr = new RoleManager<IdentityRole>(roleStore);
// Then, you create the "canEdit" role if it doesn't already exist.
if (!roleMgr.RoleExists("canEdit"))
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "canEdit" });
// Create a UserManager object based on the UserStore objcet and the ApplicationDbContext objcet.
// Note that you can create new objects and use them as parameters in a single line of code, rather than using multiple lines of code, as you did for the RoleManager object.
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var appUser = new ApplicationUser
{
UserName = "[email protected]",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "Pa$$word1");
// If the new "canEdit" user was successfully created, add the "canEdit" user to the "canEdit" role.
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "canEdit"))
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "canEdit");
}
示例5: AddUserAndRole
internal void AddUserAndRole()
{
// access the application context and create result variables.
Models.ApplicationDbContext context = new ApplicationDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
// create roleStore object that can only contain IdentityRole objects by using the ApplicationDbContext object.
var roleStore = new RoleStore<IdentityRole>(context);
var roleMgr = new RoleManager<IdentityRole>(roleStore);
// create admin role if it doesn't already exist
if (!roleMgr.RoleExists("admin"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "admin" });
}
// create a UserManager object based on the UserStore object and the ApplicationDbContext object.
// defines admin email account
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var appUser = new ApplicationUser
{
UserName = "[email protected]",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "Pa$$word1");
// If the new admin user was successfully created, add the new user to the "admin" role.
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "admin"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "admin");
}
}
示例6: AddUserAndRole
internal void AddUserAndRole()
{
ApplicationDbContext context = new ApplicationDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
var roleStore = new RoleStore<IdentityRole>(context);
var roleMgr = new RoleManager<IdentityRole>(roleStore);
if (!roleMgr.RoleExists("Admin"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "Admin" });
}
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var appUser = new ApplicationUser
{
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "adminA123...");
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "Admin"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "canEdit");
}
}
示例7: AddToNormalUserRole
internal void AddToNormalUserRole(ApplicationUser user)
{
Models.ApplicationDbContext context = new ApplicationDbContext();
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
if (!userMgr.IsInRole(userMgr.FindByEmail(user.Email).Id, "Normal"))
{
userMgr.AddToRole(userMgr.FindByEmail(user.Email).Id, "Normal");
}
}
示例8: Seed
protected override void Seed(Context context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
var userStore = new UserStore<ApplicationUser>(context);
var mngr = new UserManager<ApplicationUser>(userStore);
context.Roles.AddOrUpdate(r => r.Name, new IdentityRole { Name = "Administrators" });
ApplicationUser adm = new ApplicationUser();
adm.Email = "[email protected]";
adm.UserName = "admin";
mngr.Create(adm, "Adm!n0");
context.SaveChanges();
IdentityRole adrol = context.Roles.First(x => x.Name == "Administrators");
adm = mngr.FindByEmail("[email protected]");
mngr.AddToRole(adm.Id, adrol.Name);
context.SaveChanges();
}
示例9: Validate
public override void Validate(string userNameOrEmail, string password)
{
try
{
using (var context = new IdentityDbContext())
{
using (var userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(context)))
{
string userName = userNameOrEmail;
if (userNameOrEmail.Contains('@'))
{
var userForEmail = userManager.FindByEmail(userNameOrEmail);
if (userForEmail != null)
{
userName = userForEmail.UserName;
}
}
var user = userManager.Find(userName, password);
if (user == null)
{
var msg = String.Format("Unknown Username {0} or incorrect password {1}", userNameOrEmail, password);
Trace.TraceWarning(msg);
throw new FaultException(msg);
}
}
}
}
catch (Exception e)
{
var msg = e.Message;
Trace.TraceWarning(msg);
throw new FaultException(msg);
}
}
示例10: getUserId
public string getUserId(string user_name)
{
Models.ApplicationDbContext context = new ApplicationDbContext();
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var Id = userMgr.FindByEmail(user_name).Id;
return Id;
}
示例11: GenerateCompany
/// <summary>
/// This will generate a new company from a view company
/// </summary>
/// <param name="comp">the company to be generated</param>
/// <returns>the company that should be added to the database</returns>
public Company GenerateCompany(ViewCompany comp, int _lenghtOfPassword, int _numberOfAlphabeticCharacters)
{
var result = new Company() {Active = true, Name = comp.Name, PhoneNr = comp.PhoneNr};
//Generates a random password, and makes a new user with the correct email
var password = System.Web.Security.Membership.GeneratePassword(_lenghtOfPassword, _numberOfAlphabeticCharacters);
var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new DAL.Context.Context()));
var user = new ApplicationUser()
{
UserName = comp.Email,
Email = comp.Email
};
um.Create(user, password);
//Generates the access string by encoding the email and the password into an array of bytes.
var bytes = System.Text.Encoding.UTF8.GetBytes(comp.Email + ":" + password);
result.AccessString = System.Convert.ToBase64String(bytes);
result.Active = true;
//This will be so that the database will make the relation between the two.
var item = um.FindByEmail(comp.Email);
result.IdentityId = item.Id;
return result;
}
示例12: Create
/*This should be removed after the first admin gets made */
// GET: MakeMeAdmin/Create
public ActionResult Create(string email)
{
using (var context = new ApplicationDbContext())
{
var fadmin = context.KeyValueSettings.FirstOrDefault(s => s.Key == "FirstAdminSet");
if (fadmin == null || fadmin.Value == "false")
{
var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
roleManager.Create(new IdentityRole("Admin"));
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
var user = userManager.FindByEmail(email);
userManager.AddToRole(user.Id, "Admin");
if (fadmin == null)
{
context.KeyValueSettings.Add(new KeyValueSettings() { Key = "FirstAdminSet", Value = "true" });
}
else
{
fadmin.Value = "true";
}
context.SaveChanges();
return Json(true, JsonRequestBehavior.AllowGet);
}
}
return Json(false, JsonRequestBehavior.AllowGet);
}
示例13: AddUserAndRole
internal void AddUserAndRole()
{
// Access the application context and create result variables.
Models.ApplicationDbContext context = new ApplicationDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
// Create a RoleStore object by using the ApplicationDbContext object.
// The RoleStore is only allowed to contain IdentityRole objects.
var roleStore = new RoleStore<IdentityRole>(context);
// Create a RoleManager object that is only allowed to contain IdentityRole objects.
// When creating the RoleManager object, you pass in (as a parameter) a new RoleStore object.
var roleMgr = new RoleManager<IdentityRole>(roleStore);
// Then, you create the "canEdit" role if it doesn't already exist.
if (!roleMgr.RoleExists("SuperAdmin"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "SuperAdmin" });
}
if (!roleMgr.RoleExists("RegUser"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "RegUser" });
}
// Create a UserManager object based on the UserStore object and the ApplicationDbContext
// object. Note that you can create new objects and use them as parameters in
// a single line of code, rather than using multiple lines of code, as you did
// for the RoleManager object.
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
if (userMgr.FindByEmail("[email protected]") == null)
{
var appUser = new ApplicationUser
{
UserName = "AdminSw",
Email = "[email protected]"
};
IdUserResult = userMgr.Create(appUser, "Pantelic93.");
}
// If the new "canEdit" user was successfully created,
// add the "canEdit" user to the "canEdit" role.
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "SuperAdmin"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "SuperAdmin");
}
}
示例14: AddUserAndRole
internal void AddUserAndRole()
{
// Access the application context and create result variables.
GolddiggerDbContext context = new GolddiggerDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
// Create a RoleStore object by using the ApplicationDbContext object.
// The RoleStore is only allowed to contain IdentityRole objects.
var roleStore = new RoleStore<IdentityRole>(context);
// Create a RoleManager object that is only allowed to contain IdentityRole objects.
// When creating the RoleManager object, you pass in (as a parameter) a new RoleStore object.
var roleMgr = new RoleManager<IdentityRole>(roleStore);
// Then, you create the "admin" role if it doesn't already exist.
if (!roleMgr.RoleExists("admin"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "admin" });
}
// Create a UserManager object based on the UserStore object and the ApplicationDbContext
// object. Note that you can create new objects and use them as parameters in
// a single line of code, rather than using multiple lines of code, as you did
// for the RoleManager object.
var userMgr = new UserManager<User>(new UserStore<User>(context));
var appUser = new User
{
UserName = "admin",
Email = "[email protected]",
IsFemale = true,
ProfilePhoto = new byte[8]
};
IdUserResult = userMgr.Create(appUser, "Pa$$word1");
// If the new "admin" user was successfully created,
// add the "admin" user to the "admin" role.
if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "admin"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "admin");
}
}
示例15: Adminator
public ActionResult Adminator(string email)
{
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
roleManager.Create(new IdentityRole("Administrator"));
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
var user = userManager.FindByEmail(email);
userManager.AddToRole(user.Id, "Administrator");
return View();
}