locked
what are the IDENTITY counterparts to these outdated SQL-membership functions RRS feed

  • Question

  • User297437924 posted

    The following are all SQL-Membership functions that I have been using in my website.   I now want to port to IDENTITY, but I don't know what to replace them with:

    Roles.GetRolesForUser(username) returns list
    Roles.AddUserToRole(username,role)
    Roles.removeUserFromRole(username,role)
    Roles.IsUserInRole("zowner")
    membership.getuser()
    Membership.getallusers()
    Membership.deleteuser(username)
    email - Membership.getusernamebyemail(username)
    Membership.getcreateusererrormessage
    Membership.generatepassword
    Membership.createuser()
    Membership.validateuser(username,password
    Membership.getuser().username
    Membership.updateuser(newusers)
    getuser(username).isapproved
    Membership.enablepassword
    FormsAuthentication.signout()
    FormsAuthentication.timeout.totalminutes
    FormsAuthentication.requireSSL
    FormsAuthentication.RedirectToLoginPage

    My question is, are there IDENTITY counterparts to these, and if so, how do I look them up?

    Thursday, February 22, 2018 11:00 AM

Answers

  • User475983607 posted

    My question is, are there IDENTITY counterparts to these, and if so, how do I look them up?

    Identity is just an API that provides access data stores.  You need to read the reference docs.

    https://www.asp.net/identity

    The main APIs are the UserManager and SignInManager.  

    UserManager

    https://msdn.microsoft.com/en-us/library/dn613290(v=vs.108).aspx

    SignInManager

    https://msdn.microsoft.com/en-us/library/dn896559(v=vs.108).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 22, 2018 12:16 PM
  • User283571144 posted

    Hi RateFor,

    The following are all SQL-Membership functions that I have been using in my website.   I now want to port to IDENTITY, but I don't know what to replace them with:

    According to your description, not all membership function could be mapped in the identity. 

    I suggest you could refer to below codes:

    Roles.GetRolesForUser(username) returns list
    -------------
    public async string GetUserRole(string EmailID, string Password)
    {
        var user = await _userManager.FindAsync(EmailID, Password);
        string rolename = await _userManager.GetRoles(user.Id).FirstOrDefault();
        return rolename;
    }
    Roles.AddUserToRole(username,role)
    -----------
        internal void AddUserToRole(string userName, string roleName)
        {
            var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
    
            try
            {
                var user = UserManager.FindByName(userName);
                UserManager.AddToRole(user.Id, roleName);
                context.SaveChanges();
            }
            catch
            {
                throw;
            }
        }
    Roles.removeUserFromRole(username,role)
    ------------------
    public async Task DeleteRolesAsync(List<string> deleteList, int? userId)
    {
        if (userId != null)
        {
            foreach (var roleName in deleteList)
            {
                IdentityResult deletionResult = await UserManager.RemoveFromRoleAsync(userId, roleName);
            }
        }
    }
    Roles.IsUserInRole("zowner")
    -------------
    User.IsInRole("rolename");
    
    membership.getuser()
    ------------
    var user = UserManager.FindById(User.Identity.GetUserId());
    Membership.getallusers()
    ---------------
    //Use EF
    UsersContext = new ApplicationDbContext();
    ...
    UsersContext.Users.ToList();
    Membership.deleteuser(username)
    ----
     await _userManager.DeleteAsync(user);
    email - Membership.getusernamebyemail(username)
    -------------
    ApplicationUser user = UserManager.FindByName(userName);
    string mail= user.Email;
    Membership.createuser()
    ----------------
    var user = model.GetUser();
    var result = await UserManager.CreateAsync(user, model.Password);
    
    if (result.Succeeded)
        result = UserManager.AddToRole(user.Id, "User");
    Membership.updateuser(newusers)
    -----------------
    // Get the existing student from the db
    var user = (Student)UserManager.FindById(model.Id);
    
    // Update it with the values from the view model
    user.Name = model.Name;
    user.Surname = model.Surname;
    user.UserName = model.UserName;
    user.Email = model.Email;
    user.PhoneNumber = model.PhoneNumber;
    user.Number = model.Number; //custom property
    user.PasswordHash = checkUser.PasswordHash;
    
    // Apply the changes if any to the db
    UserManager.Update(user);
    

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 23, 2018 2:25 AM

All replies

  • User475983607 posted

    My question is, are there IDENTITY counterparts to these, and if so, how do I look them up?

    Identity is just an API that provides access data stores.  You need to read the reference docs.

    https://www.asp.net/identity

    The main APIs are the UserManager and SignInManager.  

    UserManager

    https://msdn.microsoft.com/en-us/library/dn613290(v=vs.108).aspx

    SignInManager

    https://msdn.microsoft.com/en-us/library/dn896559(v=vs.108).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 22, 2018 12:16 PM
  • User283571144 posted

    Hi RateFor,

    The following are all SQL-Membership functions that I have been using in my website.   I now want to port to IDENTITY, but I don't know what to replace them with:

    According to your description, not all membership function could be mapped in the identity. 

    I suggest you could refer to below codes:

    Roles.GetRolesForUser(username) returns list
    -------------
    public async string GetUserRole(string EmailID, string Password)
    {
        var user = await _userManager.FindAsync(EmailID, Password);
        string rolename = await _userManager.GetRoles(user.Id).FirstOrDefault();
        return rolename;
    }
    Roles.AddUserToRole(username,role)
    -----------
        internal void AddUserToRole(string userName, string roleName)
        {
            var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
    
            try
            {
                var user = UserManager.FindByName(userName);
                UserManager.AddToRole(user.Id, roleName);
                context.SaveChanges();
            }
            catch
            {
                throw;
            }
        }
    Roles.removeUserFromRole(username,role)
    ------------------
    public async Task DeleteRolesAsync(List<string> deleteList, int? userId)
    {
        if (userId != null)
        {
            foreach (var roleName in deleteList)
            {
                IdentityResult deletionResult = await UserManager.RemoveFromRoleAsync(userId, roleName);
            }
        }
    }
    Roles.IsUserInRole("zowner")
    -------------
    User.IsInRole("rolename");
    
    membership.getuser()
    ------------
    var user = UserManager.FindById(User.Identity.GetUserId());
    Membership.getallusers()
    ---------------
    //Use EF
    UsersContext = new ApplicationDbContext();
    ...
    UsersContext.Users.ToList();
    Membership.deleteuser(username)
    ----
     await _userManager.DeleteAsync(user);
    email - Membership.getusernamebyemail(username)
    -------------
    ApplicationUser user = UserManager.FindByName(userName);
    string mail= user.Email;
    Membership.createuser()
    ----------------
    var user = model.GetUser();
    var result = await UserManager.CreateAsync(user, model.Password);
    
    if (result.Succeeded)
        result = UserManager.AddToRole(user.Id, "User");
    Membership.updateuser(newusers)
    -----------------
    // Get the existing student from the db
    var user = (Student)UserManager.FindById(model.Id);
    
    // Update it with the values from the view model
    user.Name = model.Name;
    user.Surname = model.Surname;
    user.UserName = model.UserName;
    user.Email = model.Email;
    user.PhoneNumber = model.PhoneNumber;
    user.Number = model.Number; //custom property
    user.PasswordHash = checkUser.PasswordHash;
    
    // Apply the changes if any to the db
    UserManager.Update(user);
    

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 23, 2018 2:25 AM