コード例 #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();
        }