public async Task<IdentityResult> RegisterUser(BlogRegisterModel userModel)
        {
            var user = new BlogUser
            {
                UserName = userModel.Username,
                Email = userModel.Email
            };
 
            var result = await _userManager.CreateAsync(user, userModel.Password);

            return result;
        }
Beispiel #2
0
        public async Task<User> AddBlogUser(BlogRegisterModel model)
        {
            var identityUser = await UserManager.FindByNameAsync(model.Username);
            var blogUser = UsersResource.Add(new User
            {
                UserName = model.Username,
                FirstName = model.FirstName,
                LastName = model.LastName,
                BirthDate = model.BirthDate,
                IdentityId = identityUser.Id,
                EmailAddress = model.Email
            });

            return blogUser;
        }
Beispiel #3
0
		private static async Task LoadIdentities()
		{
			foreach (var user in _users)
			{
				var bloguser = new BlogRegisterModel
				{
					Username = user.UserName,
					Email = user.EmailAddress,
					Password = "******"
				};
				var result = await BlogDbRepository.RegisterUser(bloguser);
				if (result.Succeeded)
				{
					var savedUser = await BlogDbRepository.FindUser(bloguser.Username, bloguser.Password);
					var firstOrDefault = BlogDbRepository.GetRoles().FirstOrDefault();
					if (firstOrDefault != null)
					{
						var role = firstOrDefault.Name;
						await BlogDbRepository.AddToRolesAsync(savedUser.Id, new[] { role });
					}
				}
			}

			AddConsoleMessage("Successfully added users...");
		}
Beispiel #4
0
		private static async void InitializeApp()
		{
			Rollback();

			await LoadRoles();

			var bloguser = new BlogRegisterModel
			{
				Username = "******",
				Email = "*****@*****.**",
				Password = "******"
			};

			var result = await BlogDbRepository.RegisterUser(bloguser);
			if (result.Succeeded)
			{
				var savedUser = await BlogDbRepository.FindUser(bloguser.Username, bloguser.Password);
				var firstOrDefault = BlogDbRepository.GetRoles().FirstOrDefault();
				if (firstOrDefault != null)
				{
					var role = firstOrDefault.Name;
					await BlogDbRepository.AddToRolesAsync(savedUser.Id, new[] { role });
				}
			}

			UserRepository.Add(new User
			{
				FirstName = "Jason",
				LastName = "Magpantay",
				UserName = "******",
				IdentityId = Guid.NewGuid().ToString(),
				EmailAddress = "admin@bloggity",
				BirthDate = DateTime.Now.AddYears(-25)
			});
		}
        public async Task<ActionResult> Register(BlogRegisterModel model)
        {
            if (!ModelState.IsValid) return View(model);

            var user = new BlogUser { UserName = model.Username, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);

            //Add User to the selected Roles 
            if (result.Succeeded)
            {
                var roleResult = await UserManager.AddToRolesAsync(user.Id, new[] { "Bloggity Staff" });
                if (!roleResult.Succeeded)
                {
                    AddErrors(roleResult);
                    return View();
                }

                var blogUser = await _userHelper.AddBlogUser(model);
                if (blogUser.Error != null)
                {
                    ViewBag.UserCreationError = blogUser.Error.Message;
                    return View(model);
                }

                await SignInAsync(user, false);
                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
            return View();
        }
        public async Task<IHttpActionResult> Register(BlogRegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var user = new BlogUser { UserName = model.Username, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded) return GetErrorResult(result);

            var rolesResult = await UserManager.AddToRolesAsync(user.Id, new[] { "Blogger" });
            if (!rolesResult.Succeeded) return GetErrorResult(result);
            
            var blogUser = await AddBlogUser(model);
            return blogUser.Error != null ? GetErrorResult(result) : Ok();
        }