当前位置: 首页>>代码示例>>C#>>正文


C# UserManager.FindByName方法代码示例

本文整理汇总了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);
        }
开发者ID:Marsh87,项目名称:MedicalInformationSystem,代码行数:32,代码来源:Doctor_DetailController.cs

示例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");
            }
        }
开发者ID:OurFirstOrgan,项目名称:FlyingSnow.Travel,代码行数:29,代码来源:RoleActions.cs

示例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");
            }
        }
开发者ID:OurFirstOrgan,项目名称:FlyingSnow.Travel,代码行数:33,代码来源:RoleActions.cs

示例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]");
        }
开发者ID:keigito,项目名称:DryLightningDetector,代码行数:50,代码来源:Seeder.cs

示例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);
            }
        }
开发者ID:tematre,项目名称:UniversitySite,代码行数:34,代码来源:CreateRoles.cs

示例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");
            }
        }
开发者ID:CRprogrammer,项目名称:HotelPuraVida,代码行数:27,代码来源:Global.asax.cs

示例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;
                }
            }
        }
开发者ID:DanMoyer,项目名称:PageMonitor,代码行数:35,代码来源:UserRoleHelper.cs

示例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;
        }
    }
开发者ID:kgibson5,项目名称:ClassDemos,代码行数:28,代码来源:DefaultSecurity.aspx.cs

示例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;
 }
开发者ID:darkyshiny,项目名称:Personalsystem,代码行数:7,代码来源:PMRepo.cs

示例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"));
		}
开发者ID:vitalzhuravkov,项目名称:QuestionsAndAnswers,代码行数:25,代码来源:Global.asax.cs

示例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"));
        }
开发者ID:vadimberkut,项目名称:dias-schedule,代码行数:25,代码来源:ScheduleController.cs

示例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();

        }
开发者ID:railsuser2014,项目名称:asp.net-1,代码行数:26,代码来源:RepliesController.cs

示例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);
        }
开发者ID:krunalm,项目名称:MVCIdentity,代码行数:26,代码来源:HomeController.cs

示例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;
        }
开发者ID:rswetnam,项目名称:GoodBoating,代码行数:27,代码来源:SeedServices.cs

示例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);
        }
开发者ID:sophoan,项目名称:ForumWeb,代码行数:31,代码来源:QuestionController.cs


注:本文中的UserManager.FindByName方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。