本文整理汇总了C#中UserManager.FindByName方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.FindByName方法的具体用法?C# UserManager.FindByName怎么用?C# UserManager.FindByName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserManager
的用法示例。
在下文中一共展示了UserManager.FindByName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Create
public async Task<ActionResult> Create(DoctorViewModel DoctorViewModel)
{
if (ModelState.IsValid)
{
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var user = new ApplicationUser() { UserName = DoctorViewModel.Email };
var result = await UserManager.CreateAsync(user, DoctorViewModel.Password);
string roleName = "Doctor";
IdentityResult roleResult;
if (!RoleManager.RoleExists(roleName))
{
roleResult = RoleManager.Create(new IdentityRole(roleName));
}
try
{
var findUser = UserManager.FindByName(DoctorViewModel.Email);
UserManager.AddToRole(findUser.Id, "Doctor");
context.SaveChanges();
}
catch
{
throw;
}
Doctor_Detail doctor = MapDoctor(DoctorViewModel);
db.Doctor_Details.Add(doctor);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(DoctorViewModel);
}
示例2: AddUserAndRole
internal void AddUserAndRole()
{
Models.ApplicationDbContext context = new Models.ApplicationDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
var roleStore = new RoleStore<IdentityRole>(context);
var roleMgr = new RoleManager<IdentityRole>(roleStore);
if (!roleMgr.RoleExists("administrator"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "administrator" });
}
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var appUser = new ApplicationUser
{
UserName = "administrator",
};
IdUserResult = userMgr.Create(appUser, "1qaz2wsxE");
var user = userMgr.FindByName("administrator");
if (!userMgr.IsInRole(user.Id, "administrator"))
{
//userMgr.RemoveFromRoles(user.Id, "read", "edit");
IdUserResult = userMgr.AddToRole(userMgr.FindByName("administrator").Id, "administrator");
}
}
示例3: AddUserAndRole
internal void AddUserAndRole()
{
Models.ApplicationDbContext context = new Models.ApplicationDbContext();
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
var roleStore = new RoleStore<IdentityRole>(context);
var roleMgr = new RoleManager<IdentityRole>(roleStore);
if (!roleMgr.RoleExists("administrator"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "administrator" });
}
var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var appUser = new ApplicationUser
{
UserName = "administrator",
ImgUrl = "user2-160x160.jpg",
Description = "High Level",
SinceDate = new DateTime(2016, 1, 1)
};
IdUserResult = userMgr.Create(appUser, "1qaz2wsxE");
var user = userMgr.FindByName("administrator");
if (!userMgr.IsInRole(user.Id, "administrator"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByName("administrator").Id, "administrator");
}
}
示例4: Seed
public static void Seed(ApplicationDbContext context)
{
UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(context);
UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);
RoleStore<Role> roleStore = new RoleStore<Role>(context);
RoleManager<Role> roleManager = new RoleManager<Role>(roleStore);
if (!roleManager.RoleExists("Admin"))
roleManager.Create(new Role { Name = "Admin" });
if (!roleManager.RoleExists("User"))
roleManager.Create(new Role { Name = "User" });
IdentityResult result = null;
ApplicationUser user1 = userManager.FindByName("[email protected]");
if (user1 == null)
{
user1 = new ApplicationUser { Email = "[email protected]", UserName = "[email protected]" };
}
result = userManager.Create(user1, "asdfasdf");
if (!result.Succeeded)
{
string error = result.Errors.FirstOrDefault();
throw new Exception(error);
}
userManager.AddToRole(user1.Id, "Admin");
user1 = userManager.FindByName("[email protected]");
ApplicationUser user2 = userManager.FindByName("[email protected]");
if (user2 == null)
{
user2 = new ApplicationUser { Email = "[email protected]", UserName = "[email protected]" };
}
result = userManager.Create(user2, "asdfasfd");
if (!result.Succeeded)
{
string error = result.Errors.FirstOrDefault();
throw new Exception(error);
}
userManager.AddToRole(user2.Id, "User");
user2 = userManager.FindByName("[email protected]");
}
示例5: Start
public static void Start()
{
using (var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new UsersDbContext())))
{
foreach (var roleName in RolesList.Where(roleName => !roleManager.RoleExists(roleName)))
{
roleManager.Create(new IdentityRole(roleName));
}
}
using (
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new UsersDbContext()))
)
{
if (userManager.FindByName(Constants.AdminUserName) != null)
{
return;
}
var admin = new ApplicationUser {UserName = Constants.AdminUserName};
var result = userManager.Create(admin, "AdminPass");
if (!result.Succeeded)
{
var txt = new StringBuilder();
foreach (var error in result.Errors)
{
txt.AppendLine(error);
}
throw new Exception(txt.ToString());
}
userManager.AddToRole(admin.Id, Constants.Roles.Admin);
}
}
示例6: AddPermisionToADM
private void AddPermisionToADM(ApplicationDbContext db)
{
var userManarge = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
var user = userManarge.FindByName("[email protected]");
if (!userManarge.IsInRole(user.Id, "View"))
{
userManarge.AddToRole(user.Id, "View");
}
if (!userManarge.IsInRole(user.Id, "Create"))
{
userManarge.AddToRole(user.Id, "Create");
}
if (!userManarge.IsInRole(user.Id, "Edit"))
{
userManarge.AddToRole(user.Id, "Edit");
}
if (!userManarge.IsInRole(user.Id, "Delete"))
{
userManarge.AddToRole(user.Id, "Delete");
}
if (!userManarge.IsInRole(user.Id, "Adm"))
{
userManarge.AddToRole(user.Id, "Adm");
}
}
示例7: AddUserRole
public static void AddUserRole(string userName, string roleName)
{
using (var context = new ApplicationDbContext())
{
try
{
if (!context.Roles.Any(r => r.Name == roleName)) return;
var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
var store = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(store);
var user = userManager.FindByName(userName);
var role = roleManager.FindByName(roleName);
if (userManager.IsInRole(user.Id, role.Name)) return;
userManager.AddToRole(user.Id, role.Name);
context.SaveChanges();
}
catch (DbEntityValidationException ex)
{
// Retrieve the error messages as a list of strings.
// ReSharper disable once UnusedVariable
var errorMessages = ex.EntityValidationErrors
.SelectMany(x => x.ValidationErrors)
.Select(x => x.ErrorMessage);
throw;
}
}
}
示例8: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBindUserList();
DataBindRoleList();
}
//sample to check security authentication
//check the Request object that is part of every
//internet trip
if(Request.IsAuthenticated)
{
string msg = "";
msg += this.User.Identity.Name;
UserManager um = new UserManager();
var theUser = um.FindByName(this.User.Identity.Name);
if (string.IsNullOrEmpty(theUser.WaiterID.ToString()))
{
msg+= "is not a Waiter but a registered user.";
}
else
{
msg+= " has the following data: ID: " + theUser.WaiterID.ToString() + " Email: " + theUser.Email;
}
bob.Text = msg;
}
}
示例9: SetPMProperties
public PrivateMessageVM SetPMProperties(PrivateMessageVM privateMessageVM, string userid)
{
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
privateMessageVM.PM.receiverId = userManager.FindByName(privateMessageVM.UserName).Id;
privateMessageVM.PM.senderId = userManager.FindById(userid).Id;
return privateMessageVM;
}
示例10: InitDefaultUsersAndRoles
private void InitDefaultUsersAndRoles(UserManager<QuestionsAnswersUser> userManager,
RoleManager<IdentityRole> roleManager)
{
if (roleManager.FindByName("admin") == null)
{
roleManager.Create(new IdentityRole("admin"));
}
if (roleManager.FindByName("user") == null)
{
roleManager.Create(new IdentityRole("user"));
}
if (userManager.FindByName("admin") == null)
{
var user = new QuestionsAnswersUser {UserName = "admin"};
var result = userManager.Create(user, "adminadmin");
if (result.Succeeded)
{
userManager.AddToRole(user.Id, "admin");
}
}
userManager.Users.Where(u => !u.Roles.Any()).ToList().ForEach(u => userManager.AddToRole(u.Id, "user"));
}
示例11: ChangeAcessMode
public ActionResult ChangeAcessMode(ScheduleAccessMode mode)
{
ApplicationDbContext context = new ApplicationDbContext();
if (HttpContext.User.IsInRole("Admin"))
{
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
string userName = HttpContext.User.Identity.GetUserName();
var adminUser = userManager.FindByName(userName);
adminUser.ScheduleAccessMode = mode;
try
{
userManager.Update(adminUser);
}
catch (Exception ex)
{
return RedirectToAction("View","Error",new CustomError(ex.Message));
}
string previousUrl = System.Web.HttpContext.Current.Request.UrlReferrer.AbsoluteUri;
//return RedirectToAction("View", "Schedule", new { course = 1 });
return Redirect(previousUrl);
}
return RedirectToAction("View", "Error", new CustomError("Access Denied"));
}
示例12: Create
public ActionResult Create(int? Id, [Bind(Include = "Id,MessageId,Text,PublishDate")]Reply reply)
{
if (Id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
else
{
if (ModelState.IsValid)
{
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
reply.User = userManager.FindByName(User.Identity.Name);
reply.MessageId = (int)Id;
reply.PublishDate = DateTime.Now;
db.Replies.Add(reply);
db.SaveChanges();
return RedirectToAction("Details", "Messages", new { id = Id });
}
}
return View();
}
示例13: Index
public ActionResult Index()
{
MyIdentityDbContext db = new MyIdentityDbContext();
UserStore<MyIdentityUser> userStore = new UserStore<MyIdentityUser>(db);
UserManager<MyIdentityUser> userManager = new UserManager<MyIdentityUser>(userStore);
MyIdentityUser user = userManager.FindByName(HttpContext.User.Identity.Name);
NorthWindEntities northwindDb = new NorthWindEntities();
List<Customer> customers = null;
if (userManager.IsInRole(user.Id, "Administrator"))
{
customers = northwindDb.Customers.ToList();
}
if (userManager.IsInRole(user.Id, "Operator"))
{
customers = northwindDb.Customers.Where(m => m.City == "USA").ToList();
}
ViewBag.FullName = user.FullName + " (" + user.UserName + ") !";
return View(customers);
}
示例14: CreateUser
public static ApplicationUser CreateUser(UserManager<ApplicationUser> userManager, string email, string firstName, string lastName,
string password, bool lockOutEnabled)
{
var user = userManager.FindByName(email);
if (user == null)
{
user = new ApplicationUser
{
UserName = email,
Email = email,
FirstName = firstName,
LastName = lastName,
EmailConfirmed = true
};
try
{
userManager.Create(user, password);
}
catch (Exception ex)
{
Log4NetHelper.Log("Error creating Admin User", LogLevel.ERROR, "AspNetUser", 1, "none", ex);
}
userManager.SetLockoutEnabled(user.Id, lockOutEnabled);
}
return user;
}
示例15: Index
public ViewResult Index(int subid = 0, string search = "", int page = 1)
{
UserStore<User> userStore = new UserStore<User>(repository.Context);
UserManager<User> userManager = new UserManager<User>(userStore);
User user = userManager.FindByName(HttpContext.User.Identity.Name);
QuestionListViewModel model = new QuestionListViewModel();
model.Questions = repository.Questions
.Where(q => subid == 0 ? true : q.SubjectId == subid);
model.Questions = model.Questions
.Where(q => q.Description.Contains(search));
model.PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = model.Questions.Count()
};
model.Questions = model.Questions
.OrderByDescending(q => q.PublishedAt)
.Skip((page - 1) * PageSize)
.Take(PageSize);
model.SubjectId = subid;
model.Search = search;
model.questionAddViewModel = new QuestionAddViewModel();
model.questionAddViewModel.Subjects = repository.Subjects
.OrderBy(s => s.Year);
return View(model);
}