Beispiel #1
0
        private void SeedRolesAndUsers(ETCCanadaContext context)
        {
            RoleManager<ApplicationRole> roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(context));

            if (!roleManager.RoleExists("Administrator"))
            {
                roleManager.Create(new ApplicationRole("Administrator"));
            }

            if (!roleManager.RoleExists("User"))
            {
                roleManager.Create(new ApplicationRole("User"));
            }

            List<ApplicationUser> users = new List<ApplicationUser>();
            ApplicationUser user1 = new ApplicationUser { UserName = "******", Email = "*****@*****.**", FirstName = "Administrator", LastName = "Administrator", IsApproved = true };
            ApplicationUser user2 = new ApplicationUser { UserName = "******", Email = "*****@*****.**", FirstName = "User", LastName = "User", IsApproved = true };
            users.Add(user1);
            users.Add(user2);

            SeedDefaultUser(context, user1, "Administrator");
            SeedDefaultUser(context, user2, "User");

            //http://stackoverflow.com/questions/20470623/asp-net-mvc-asp-net-identity-seeding-roles-and-users
            //http://stackoverflow.com/questions/19280527/mvc5-seed-users-and-roles
            //http://stackoverflow.com/questions/19745286/asp-net-identity-db-seed
        }
        internal static ApplicationUserModel ToApplicationUserModel(ApplicationUser user)
        {
            return new ApplicationUserModel
            {
                AddressLine1 = user.AddressLine1,
                AddressLine2 = user.AddressLine2,
                City = user.City,
                Country = user.Country,
                DisplayName = user.DisplayName,
                Email = user.Email,
                FirstName = user.FirstName,
                // Gender = user.Profile.Gender.HasValue? (Gender?)user.Profile.Gender.Value : null,
                Id = user.Id,
                LastName = user.LastName,
                Phone = user.Phone,
                PostalCode = user.PostalCode,
                State = user.State,
                UserName = user.UserName,
                IsApproved = user.IsApproved,
                FirstNameInTamil = user.FirstNameInTamil,
                LastNameInTamil = user.LastNameInTamil
                // RoleName = IdentityManagers.UserManager.GetRoles(user.Id).First() This line throws a NullReferenceException for UserManager, Have to Check why? https://aspnetidentity.codeplex.com/workitem/2179

            };
        }
 internal static ApplicationUserModel ToApplicationUserMetaOnLogin(ApplicationUser user)
 {
     return new ApplicationUserModel
     {
         Id = user.Id,
         Email = user.Email,
         FirstName = user.FirstName,
         LastName = user.LastName,
         UserName = user.UserName,
         RoleName = "Adminstrator"// TODO: Get from DB, maintain Roles[] to support multiple roles
         // RoleName = IdentityManagers.UserManager.GetRoles(user.Id).First() This line throws a NullReferenceException for UserManager, Have to Check why? https://aspnetidentity.codeplex.com/workitem/2179
     };
 }
Beispiel #4
0
        private bool SeedDefaultUser(ETCCanadaContext context, ApplicationUser user, string role)
        {
            UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

            if (userManager.FindByName(user.UserName) == null)
            {
                var result = userManager.Create(user, "123123");

                if (result.Succeeded)
                {
                    userManager.AddToRole(user.Id, role);
                    return true;
                }
            }
            return false;
        }
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index", "Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
        internal static RegisterModel ToRegisterModel(ApplicationUser user)
        {
            return new RegisterModel
            {
                Email = user.Email,
                FirstName = user.FirstName,
                LastName = user.LastName,
                IsApproved = false,
                Country = user.Country,
                FirstNameInTamil = user.FirstNameInTamil,
                LastNameInTamil = user.LastNameInTamil

            };
        }