本文整理汇总了C#中ApplicationUserManager.GetRolesAsync方法的典型用法代码示例。如果您正苦于以下问题:C# ApplicationUserManager.GetRolesAsync方法的具体用法?C# ApplicationUserManager.GetRolesAsync怎么用?C# ApplicationUserManager.GetRolesAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ApplicationUserManager
的用法示例。
在下文中一共展示了ApplicationUserManager.GetRolesAsync方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Users
public async Task<ActionResult> Users()
{
userManager = new ApplicationUserManager(new ApplicationUserStore(identityDb));
var users = identityDb.Users.ToList();
var models = new List<UserViewModel>();
foreach (var user in users)
{
models.Add(new UserViewModel
{
UserId = user.Id,
Roles = await userManager.GetRolesAsync(user.Id),
UserName = user.UserName
});
}
return View(models);
}
示例2: InsertOrUpdate
public async Task<bool> InsertOrUpdate(IdentityUserViewModel model, ApplicationUserManager userManager)
{
var user = await userManager.FindByIdAsync(model.Id);
if (user == null)
{
user = new ApplicationUser();
user.Assign(model);
var result = await userManager.CreateAsync(user, "1234567");
if (!result.Succeeded) return false;
model.Id = user.Id;
}
else
{
user.Email = model.Email;
user.UserName = model.UserName;
user.DUI = model.DUI;
user.PhoneNumber = model.PHONE_2;
user.ADDRESS = model.ADDRESS;
user.Category = model.Category;
user.FirstName = model.FirstName;
user.LastName = model.LastName;
user.DUI = model.DUI;
user.PHONE_2 = model.PHONE_2;
user.ProfilePicture = model.ProfilePicture;
user.CenterId = model.CenterId;
//user.Address = model.Address;
//user.FirstName = model.FirstName;
//user.LastName = model.LastName;
//user.DocumentNum = model.DocumentNum;
//user.ProfilePicture = model.ProfilePicture;
await userManager.UpdateAsync(user);
}
if (model.ForceChangePassword)
{
var tok = await userManager.GeneratePasswordResetTokenAsync(model.Id);
var result = await userManager.ResetPasswordAsync(model.Id, tok, model.Password);
if (!result.Succeeded) return false;
}
var roles = await userManager.GetRolesAsync(model.Id);
if (!roles.Any() && !string.IsNullOrEmpty(model.Role))
{
var res = await userManager.AddToRoleAsync(model.Id, model.Role);
}
if (roles.All(r => r != model.Role) && roles.Any())
{
var result = await userManager.AddToRoleAsync(model.Id, model.Role);
}
roles.Where(r => r != model.Role).ToList().ForEach(role => userManager.RemoveFromRole(model.Id, role));
if (model.CenterId != 0)
{
var claim = await Context.UserClaims.FirstOrDefaultAsync(c => c.UserId == user.Id && c.ClaimType == "CenterId");
var claims = await userManager.GetClaimsAsync(user.Id);
if (claim != null)
{
claim.ClaimValue = model.CenterId.Value.ToString();
await Context.SaveChangesAsync();
}
else
{
await userManager.AddClaimAsync(model.Id, new System.Security.Claims.Claim("CenterId", model.CenterId.ToString()));
}
}
return true;
}
示例3: DeleteUser
public async Task<ActionResult> DeleteUser(string Username)
{
try
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
ApplicationUser user = context.Users.Where(u => u.UserName.Equals(Username, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
string userID = user.Id;
if (ModelState.IsValid)
{
if (userID == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var userDel = await manager.FindByIdAsync(userID);
var logins = user.Logins;
foreach (var login in logins.ToList())
{
await manager.RemoveLoginAsync(login.UserId, new UserLoginInfo(login.LoginProvider, login.ProviderKey));
}
var rolesForUser = await manager.GetRolesAsync(userID);
if (rolesForUser.Count() > 0)
{
foreach (var item in rolesForUser.ToList())
{
// item should be the name of the role
var result = await manager.RemoveFromRoleAsync(user.Id, item);
}
}
await manager.DeleteAsync(user);
TempData["ValidationMessage"] = ("Success: " + " " + Username + " " + "Was Removed");
return View("ManageUser");
}
else
{
TempData["ValidationMessage"] = ("Error: " + " " + Username + " " + "Was Not Removed");
return View("ManageUser");
}
}
catch
{
TempData["ValidationMessage"] = ("Error: Something Went Wrong");
return View("ManageUser");
}
}
示例4: UpdateUserStatsInGroupAsync
public async Task<IList<Group>> UpdateUserStatsInGroupAsync(IEnumerable<String> users, Group mainGroup, ApplicationDbContext appContext, ApplicationUserManager userManager, params String[] checkableRoles)
{
if (users != null && mainGroup != null && appContext != null)
{
Course groupCourse = await appContext.Courses.FindAsync(mainGroup.Course_ID);
if (groupCourse != null)
{
IList<String> userRoles = null;
List<Group> groups = new List<Group>();
CourseUsers user;
for (Int32 i = 0; i < users.Count(); i++)
{
userRoles = await userManager.GetRolesAsync(await GetUsersIdFromName(users.ElementAt(i), appContext));
user = CheckUserForRoles(checkableRoles, userRoles, CourseUsers.Student);
IList<Group> gr = await ChangeCourseUserGroupAsync(new String[] { users.ElementAt(i) }, appContext, groupCourse, user, false);
if (gr.Count > 0)
{
CopyMainDataOfGroupTo(mainGroup, gr.First());
groups.Add(gr.First());
}
}
return groups;
}
}
return new List<Group>();
}
示例5: GetUserWithDefRoleAndCurrCourse
public async Task<IList<ApplicationUser>> GetUserWithDefRoleAndCurrCourse(ApplicationDbContext appContext, ApplicationUserManager userManager, Course course, String groupName, String userRole, params String[] groupRoles)
{
List<ApplicationUser> foundUsers = new List<ApplicationUser>();
try
{
if (appContext != null && userManager != null)
{
IList<ApplicationUser> users = await appContext.Users.ToListAsync();
/*get all teachers to List*/
IList<String> userRoles = null;
for (Int32 i = 0; i < users.Count; i++)
{
userRoles = await userManager.GetRolesAsync(users[i].Id);
if (userRoles.Contains(userRole))
foundUsers.Add(users[i]);
}
}
IEnumerable<ApplicationUser> usersWithRole = foundUsers.Where(u => CheckGroupUserForRole(u, course, groupName, groupRoles));
if (usersWithRole != null)
return usersWithRole.ToArray();
return new List<ApplicationUser>();
}
catch (InvalidOperationException)
{
return new List<ApplicationUser>();
}
catch (NullReferenceException)
{
return new List<ApplicationUser>();
}
}
示例6: GetUsersWithDefinedRoleForGroupAsync
/*
* Gets avaible users with userRole for group
*
*/
public async Task<IList<ApplicationUser>> GetUsersWithDefinedRoleForGroupAsync(ApplicationDbContext appContext, ApplicationUserManager userManager, String userRole = "Teacher")
{
List<ApplicationUser> foundUsers = new List<ApplicationUser>();
try
{
if (appContext != null && userManager != null)
{
IList<ApplicationUser> users = await appContext.Users.ToListAsync();
/*get all teachers to List*/
IList<String> userRoles = null;
for (Int32 i = 0; i < users.Count; i++)
{
userRoles = await userManager.GetRolesAsync(users[i].Id);
if (userRoles.Contains(userRole))
foundUsers.Add(users[i]);
}
}
return foundUsers;
}
catch (InvalidOperationException)
{
return foundUsers;
}
catch (NullReferenceException)
{
return foundUsers;
}
}
示例7: FilterUsersByRole
public async Task<IList<ApplicationUser>> FilterUsersByRole(IEnumerable<ApplicationUser> users, String requiredRole, ApplicationUserManager userManager)
{
List<ApplicationUser> filteredUsers = new List<ApplicationUser>();
IList<String> userRoles = null;
foreach (var user in users)
{
userRoles = await userManager.GetRolesAsync(user.Id);
if (userRoles.Contains(requiredRole))
filteredUsers.Add(user);
}
return filteredUsers;
}
示例8: Index
// GET: ControlPanel/Users
public async Task<ActionResult> Index(string sortOrder, UserMessageId? message)
{
ViewBag.StatusMessage =
message == UserMessageId.ChangeUser ? "User has been successfully changed."
: message == UserMessageId.RemoveUser ? "User has been successfully removed."
: message == UserMessageId.Error ? "An error has occurred."
: "";
if (message == UserMessageId.Error)
{
ViewBag.StatusClass = "alert-danger";
}
else
{
ViewBag.StatusClass = "alert-success";
}
List<UserListViewModel> userList = new List<UserListViewModel>();
var users = new List<MyUser>();
using (var db = new ApplicationDbContext())
{
users = await db.Users.OrderBy(m => m.Id).ToListAsync();
foreach (MyUser singleUser in users)
{
IList<string> userRole;
using (var userManager = new ApplicationUserManager(new MyUserStore(new ApplicationDbContext())))
{
userRole = await userManager.GetRolesAsync(singleUser.Id);
};
var user = new UserListViewModel
{
UserId = singleUser.Id,
UserName = singleUser.UserName,
FirstName = singleUser.UserDetails.FirstName,
LastName = singleUser.UserDetails.LastName,
Role = userRole.Count > 0 ? userRole[0].ToString() : ""
};
userList.Add(user);
}
}
ViewBag.SortById = String.IsNullOrEmpty(sortOrder) ? "SortByIdDesc" : "SortByIdAsc";
ViewBag.SortByUsername =
sortOrder == "SortByUsernameAsc" ? "SortByUsernameDesc" : "SortByUsernameAsc";
switch (sortOrder)
{
case "SortByIdAsc":
return View(userList.OrderBy(m => m.UserId));
case "SortByIdDesc":
return View(userList.OrderByDescending(m => m.UserId));
case "SortByUsernameAsc":
return View(userList.OrderBy(m => m.UserName));
case "SortByUsernameDesc":
return View(userList.OrderByDescending(m => m.UserName));
}
return View(userList);
}