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


C# ApplicationUserManager.FindByName方法代码示例

本文整理汇总了C#中ApplicationUserManager.FindByName方法的典型用法代码示例。如果您正苦于以下问题:C# ApplicationUserManager.FindByName方法的具体用法?C# ApplicationUserManager.FindByName怎么用?C# ApplicationUserManager.FindByName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ApplicationUserManager的用法示例。


在下文中一共展示了ApplicationUserManager.FindByName方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateUsersForReport

 public static void CreateUsersForReport(ApplicationDbContext context, string usersstring)
 {
     var userManager = new ApplicationUserManager(new UserStore<User>(context));
     string[][] userarray = usersstring.Split(',').Select(str => str.Split(' ')).ToArray();
     foreach (var strar in userarray)
     {
         User usersu = userManager.FindByName(strar[0] + " " + strar[1]);
         if (usersu == null)
         {
             usersu = new User
             {
                 UserName = strar[0] + " " + strar[1],
                 FirstName = strar[1],
                 LastName = strar[0],
                 SecurityStamp = Guid.NewGuid().ToString(),
                 LastLoginTime = DateTime.UtcNow,
                 RegistrationDate = DateTime.UtcNow,
                 PasswordHash =
                     userManager.PasswordHasher.HashPassword("12345")
             };
             IdentityRole role = context.Roles.FirstOrDefault(r => string.Equals(r.Name, "Employee"));
             if (role != null) usersu.Roles.Add(new IdentityUserRole {RoleId = role.Id, UserId = usersu.Id});
             context.Entry(usersu).State = EntityState.Added;
         }
     }
     context.SaveChanges();
 }
开发者ID:densem-2013,项目名称:ACSDining,代码行数:27,代码来源:Utility.cs

示例2: Create

        public ActionResult Create([Bind(Include = "Id,Date,Title,Text")] Posts posts, List<int> tags)
        {
            var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(db));

            if (ModelState.IsValid) {
                posts.User = userManager.FindByName(User.Identity.Name);

                // Связь поста с изображениями.
                List<Images> images = ImgMarkup.Matches(posts.Text);
                if (images?.Count > 0) {
                    foreach (var image in images) {
                        posts.Images.Add(db.Images.Find(image.Id));
                    }
                }

                // Связь поста с тегами.
                if (tags?.Count > 0) {
                    foreach (var id in tags) {
                        Tags tag = db.Tags.Find(id);
                        if (tag == null) {
                            ModelState.AddModelError("", "Выбран несуществующий тег.");
                            return View(posts);
                        }
                        posts.Tags.Add(tag);
                    }
                }

                db.Posts.Add(posts);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(posts);
        }
开发者ID:SokolovArtur,项目名称:Blog,代码行数:33,代码来源:PostsController.cs

示例3: Post

        public ActionResult Post([Bind(Include = "Id,Date,Comment")] Comments form, int? id)
        {
            if (id == null) {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Posts post = db.Posts.Find(id);
            if (post == null) {
                return HttpNotFound();
            }

            /*
            Вывод ошибки в общий блок.
            if (form.Comment.ToLower().Contains("admin")) {
                ModelState.AddModelError("", "В нашем блоге нельзя упоминать всевышнего.");
            }
            */

            var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(db));

            if (ModelState.IsValid) {
                Comments comment = new Comments() {
                    Date = DateTime.Now,
                    Comment = form.Comment,
                    User = userManager.FindByName(User.Identity.Name)
                };
                post.Comments.Add(comment);

                db.Comments.Add(comment);
                db.SaveChanges();
                return RedirectToAction("Post", new { id = post.Id });
            }

            return View(post);
        }
开发者ID:SokolovArtur,项目名称:Blog,代码行数:34,代码来源:BlogController.cs

示例4: InitializeAppEnvironment

        private void InitializeAppEnvironment()
        {
            //app environment configuration
            using (var db = new ApplicationDbContext())
            {
                db.Database.CreateIfNotExists();
                var roleStore = new RoleStore<IdentityRole>(db);
                var role = roleStore.FindByNameAsync("Admin").Result;
                if (role == null)
                {
                    roleStore.CreateAsync(new IdentityRole("Admin")).Wait();
                }

                var userStore = new UserStore<ApplicationUser>(db);
                var manager = new ApplicationUserManager(userStore);

                var admin = manager.FindByName("admin");
                if (admin == null)
                {
                    admin = new ApplicationUser
                    {
                        UserName = "admin",
                        Email = "[email protected]",
                        EmailConfirmed = true,
                        CreateDate = DateTime.Now
                    };
                    var r = manager.CreateAsync(admin, "~Pwd123456").Result;
                }
                if (!manager.IsInRole(admin.Id, role.Name))
                {
                    manager.AddToRole(admin.Id, role.Name);
                }
            }
        }
开发者ID:kissstudio,项目名称:Topawes,代码行数:34,代码来源:Startup.cs

示例5: MainTest

        public void MainTest()
        {
            var uman = new ApplicationUserManager( new UserStore<SnooNotesAPI.Models.ApplicationUser>( new ApplicationDbContext() ) );
            var ident = uman.FindByName( "snoonotes" );

            foreach(var claim in uman.GetClaims( ident.Id ) ) {
                uman.RemoveClaim( ident.Id, claim );
            }

            ident.Claims.Add( new IdentityUserClaim() { UserId = ident.Id, ClaimType = "urn:snoonotes:subreddits:goawaynoonelikesyou:admin", ClaimValue = "true" } );
            ident.Claims.Add( new IdentityUserClaim() { UserId = ident.Id, ClaimType = ClaimsIdentity.DefaultRoleClaimType, ClaimValue = "gooaway" } );

            uman.Update( ident );

            uman.Dispose();

            Program.Main( new string[] { "goawaynoonelikesyou", "gooaway","snoonotes" } );

            uman = new ApplicationUserManager( new UserStore<SnooNotesAPI.Models.ApplicationUser>( new ApplicationDbContext() ) );
            ident = uman.FindByName( "snoonotes" );
            if ( ident.Claims.Any( c => c.ClaimType == "urn:snoonotes:subreddits:goawaynoonelikesyou:admin" ) ) Assert.Fail( "Admin claim not removed." );
            if ( ident.Claims.Any( c => c.ClaimType == ClaimsIdentity.DefaultRoleClaimType && c.ClaimValue == "gooaway" ) ) Assert.Fail( "Invalid sub claim not removed" );
            if ( !ident.Claims.Any( c => c.ClaimType == ClaimsIdentity.DefaultRoleClaimType && c.ClaimValue == "snoonotes" ) ) Assert.Fail( "Access roll not added" );
            if ( !ident.Claims.Any( c => c.ClaimType == ClaimsIdentity.DefaultRoleClaimType && c.ClaimValue == "goawaynoonelikesyou" ) ) Assert.Fail( "Access roll not added" );
            if ( !ident.Claims.Any( c => c.ClaimType == "urn:snoonotes:subreddits:snoonotes:admin" && c.ClaimValue == "true" ) ) Assert.Fail( "Admin roll not added" );
        }
开发者ID:CrustyJew,项目名称:SnooNotes,代码行数:26,代码来源:ProgramTests.cs

示例6: Configuration

        public void Configuration(IAppBuilder app)
        {
            var context = new ApplicationDbContext();
            var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
            var UserManager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
            if (!RoleManager.RoleExists("Admin"))
            {
                RoleManager.Create(new IdentityRole("Admin"));
                RoleManager.Create(new IdentityRole("Teacher"));

            }
            if (UserManager.FindByName("[email protected]") == null)
            {
                var user = new ApplicationUser { Name = "管理员", UserName = "[email protected]", Email = "[email protected]" };
                UserManager.Create(user, "administrator");
                UserManager.AddToRole(UserManager.FindByName(user.UserName).Id, "Admin");
            }
            ConfigureAuth(app);
        }
开发者ID:zqiangsun,项目名称:EquipManagement,代码行数:19,代码来源:Startup.cs

示例7: Create

        public ActionResult Create([Bind(Include = "Id,Date,Comment")] Comments comments)
        {
            var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(db));

            if (ModelState.IsValid) {
                comments.User = userManager.FindByName(User.Identity.Name);
                db.Comments.Add(comments);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(comments);
        }
开发者ID:SokolovArtur,项目名称:Blog,代码行数:13,代码来源:CommentsController.cs

示例8: Post

        public void Post(RoleUsersDto dto)
        {
            var context = ApplicationDbContext.Create();
            var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
            var roleManager = new ApplicationRoleManager(new RoleStore<ApplicationRole>(context));

            if (!roleManager.RoleExists(dto.RoleName)) return;
            foreach (
                var user in
                    dto.UserNames.Select(userName => userManager.FindByName(userName))
                        .Where(user => user != null)
                        .Where(user => !userManager.IsInRole(user.Id, dto.RoleName)))
            {
                userManager.AddToRole(user.Id, dto.RoleName);
            }
            foreach (
                var user in 
                    dto.UserNames.Select(userName => userManager.FindByName(userName))
                        .Where(user => user != null)
                        .Where(user => userManager.IsInRole(user.Id, dto.RoleName)))
            {
                userManager.RemoveFromRole(user.Id, dto.RoleName);
            }
        }
开发者ID:ouyh18,项目名称:LtePlatform,代码行数:24,代码来源:ApplicationRolesController.cs

示例9: Create

        public ActionResult Create(Lecture _lecture, FormCollection collector)
        {
            BusinessLogicHandler _gateWay = new BusinessLogicHandler();

            #region Identity

            ApplicationDbContext dataSocket = new ApplicationDbContext();
            UserStore<ApplicationUser> myStore = new UserStore<ApplicationUser>(dataSocket);
            UserManager = new ApplicationUserManager(myStore);
            var user = UserManager.FindByName(HttpContext.User.Identity.Name);

            #endregion

            #region Get Lecturer
            //Lecturer staffMember = new Lecturer();
            //staffMember = _gateWay.GetLecturer(user.Id);
            #endregion

            #region Setting things up
            try
            {
                string[] dateSlice = collector.GetValue("dateHoldhidden").AttemptedValue.Split(' ');
                string timeSlice = collector.GetValue("timeHoldhidden").AttemptedValue;
                _lecture.TimeSlot = dateSlice[0] + " " + timeSlice;
            }
            catch
            {
                return View(_lecture);
            }

            #endregion
            try
            {
                if(_gateWay.InsertLecture(_lecture))
                { return RedirectToAction("Index");}
                else
                { return View(_lecture); }
            }
            catch
            {
                return View(_lecture);
            }
        }
开发者ID:Gcobani,项目名称:project-insight,代码行数:43,代码来源:LectureController.cs

示例10: UpdateModeratedSubredditsTest

		public async Task UpdateModeratedSubredditsTest() {

			var uman = new ApplicationUserManager( new UserStore<ApplicationUser>( new ApplicationDbContext(  ) ) );
			var ident = uman.FindByName( "snoonotes" );

			AuthUtils.CheckTokenExpiration( ident );

			ident.Claims.Clear();

			ident.Claims.Add( new IdentityUserClaim() { UserId = ident.Id, ClaimType = "urn:snoonotes:subreddits:goawaynoonelikesyou:admin", ClaimValue = "true" } );
			ident.Claims.Add( new IdentityUserClaim() { UserId = ident.Id, ClaimType = ClaimsIdentity.DefaultRoleClaimType, ClaimValue = "gooaway" } );

			await AuthUtils.UpdateModeratedSubreddits( ident, uman );

			if ( ident.Claims.Any( c => c.ClaimType == "urn:snoonotes:subreddits:goawaynoonelikesyou:admin" ) ) Assert.Fail( "Admin claim not removed." );
			if ( ident.Claims.Any( c => c.ClaimType == ClaimsIdentity.DefaultRoleClaimType && c.ClaimValue == "gooaway" ) ) Assert.Fail( "Invalid sub claim not removed" );
			if ( !ident.Claims.Any( c => c.ClaimType == ClaimsIdentity.DefaultRoleClaimType && c.ClaimValue == "snoonotes" ) ) Assert.Fail( "Access roll not added" );
			if ( !ident.Claims.Any( c => c.ClaimType == ClaimsIdentity.DefaultRoleClaimType && c.ClaimValue == "goawaynoonelikesyou" ) ) Assert.Fail( "Access roll not added" );
			if ( !ident.Claims.Any( c => c.ClaimType == "urn:snoonotes:subreddits:snoonotes:admin" && c.ClaimValue == "true" ) ) Assert.Fail( "Admin roll not added" );

		}
开发者ID:CrustyJew,项目名称:SnooNotes,代码行数:21,代码来源:AuthUtilsTests.cs

示例11: AnswersFilter

        private AnswersViewModel AnswersFilter(int categoryId)
        {
            userManager = new ApplicationUserManager(new ApplicationUserStore(identityDb));
            var user = userManager.FindByName(User.Identity.Name);
            var userId = user.Id;
            if (user.SelectedPeriodId == null)
                return null;

            var periodId = (int)user.SelectedPeriodId;

            var answers = db.Answer.Where(a => a.PeriodId == periodId && a.Question.CategoryId == categoryId && a.UserId == userId).ToList();
            var questions = db.Question.Where(q => q.CategoryId == categoryId).ToList();

            var missingQuestionIds = questions.Select(q => q.Id).Except(answers.Select(a => a.QuestionId));

            foreach (var id in missingQuestionIds)
            {
                db.Answer.Add(new Answer { QuestionId = id, Value = 0, PeriodId = periodId, UserId = userId });
            }
            db.SaveChanges();

            answers = db.Answer.Where(a => a.PeriodId == periodId && a.Question.CategoryId == categoryId && a.UserId == userId).ToList();

            var model = new AnswersViewModel();
            model.Answers = answers;
            return model;
        }
开发者ID:jienfei,项目名称:nspoc,代码行数:27,代码来源:MaturityScanController.cs


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