예제 #1
0
        public static ApplicationUser GetUser(string Id)
        {
            var store   = new UserStore <ApplicationUser>(UserEntities.Create());
            var manager = new UserManager <ApplicationUser>(store);

            return(manager.FindById(Id));
        }
예제 #2
0
 public static List <IdentityRole> GetAllRoles()
 {
     using (var entities = UserEntities.Create())
     {
         return(entities.Roles.ToList());
     }
 }
예제 #3
0
        public static List <ApplicationUser> GetAllUsers()
        {
            var store   = new UserStore <ApplicationUser>(UserEntities.Create());
            var manager = new UserManager <ApplicationUser>(store);

            Users = manager.Users.ToList();
            return(Users);
        }
예제 #4
0
        public static IdentityResult ChangePassword(string email, string password, string newPassword)
        {
            var store   = new UserStore <ApplicationUser>(UserEntities.Create());
            var manager = new UserManager <ApplicationUser>(store);
            var user    = manager.FindByName(email);

            return(manager.ChangePassword(user.Id, password, newPassword));
        }
예제 #5
0
        public static IdentityResult CreateNew(string email, string password)
        {
            var store   = new UserStore <ApplicationUser>(UserEntities.Create());
            var manager = new UserManager <ApplicationUser>(store);
            var user    = new ApplicationUser {
                Email = email, UserName = email, PasswordHash = password
            };
            var result = manager.Create(user, password);

            Users.Add(user);
            return(result);
        }
예제 #6
0
        public static void AddRole(ApplicationUser user, List <string> roles)
        {
            var store   = new UserStore <ApplicationUser>(UserEntities.Create());
            var manager = new UserManager <ApplicationUser>(store);

            foreach (var role in roles)
            {
                if (!user.Roles.Any(x => x.RoleId == role))
                {
                    manager.AddToRole(user.Id, role);
                }
            }
        }
예제 #7
0
        public static IdentityResult ChangePassword(string email, string newPassword)
        {
            var store   = new UserStore <ApplicationUser>(UserEntities.Create());
            var manager = new UserManager <ApplicationUser>(store);
            var user    = manager.FindByName(email);

            var provider = new DpapiDataProtectionProvider("Sample");

            manager.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(provider.Create("EmailConfirmation"));

            var token = manager.GeneratePasswordResetToken(user.Id);

            return(manager.ResetPassword(user.Id, token, newPassword));
        }
예제 #8
0
        public static void AddRoles(ApplicationUser user, List <string> roles)
        {
            var store   = new UserStore <ApplicationUser>(UserEntities.Create());
            var manager = new UserManager <ApplicationUser>(store);

            foreach (var role in roles)
            {
                if (!user.Roles.Any(x => x.RoleId == role))
                {
                    manager.AddToRole(user.Id, role);
                }
            }
            var missingRoles = user.Roles.Where(x => roles.Contains(x.RoleId) == false);

            foreach (var role in missingRoles)
            {
                manager.RemoveFromRole(user.Id, role.RoleId);
            }
            RefreshUsers();
        }