本文整理汇总了C#中UserManager.GetRolesAsync方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.GetRolesAsync方法的具体用法?C# UserManager.GetRolesAsync怎么用?C# UserManager.GetRolesAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserManager
的用法示例。
在下文中一共展示了UserManager.GetRolesAsync方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Index
public async Task<ActionResult> Index()
{
if (this.User.Identity.IsAuthenticated)
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this.db));
var appUser = await manager.FindByIdAsync(this.User.Identity.GetUserId());
IQueryable<Game> games = from membership in db.GameMemberships
where membership.ApplicationUserID == appUser.Id
join game in db.Games on membership.GameID equals game.ID
select game;
var adminRole = await db.Roles.FirstOrDefaultAsync(role => role.Name == "Admin");
this.ViewBag.Games = await games.ToListAsync();
this.ViewBag.Claims = await manager.GetClaimsAsync(appUser.Id);
this.ViewBag.Roles = await manager.GetRolesAsync(appUser.Id);
}
return View();
}
示例2: SendAsync
protected async override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
var query = request.RequestUri.ParseQueryString();
if (query.AllKeys.Any(x => x == "token"))
{
var jwt = ((JwtSecurityToken)new JwtSecurityTokenHandler().ReadToken(query.GetValues("token")[0]));
var userId = jwt.Claims.FirstOrDefault(x => x.Type.Equals("nameid")).Value;
if (userId != null)
using (var context = DatabaseContext.Create())
using (var userManager = new UserManager(new UserStore<User>(context)))
{
User currentUser = null;
if ((currentUser = await userManager.FindByIdAsync(userId)) != null)
{
var identity = await currentUser.GenerateUserIdentityAsync(userManager, OAuthDefaults.AuthenticationType);
var principal = new GenericPrincipal(identity, (await userManager.GetRolesAsync(currentUser.Id)).ToArray());
Thread.CurrentPrincipal = principal;
HttpContext.Current.User = principal;
}
}
}
return await base.SendAsync(request, cancellationToken);
}
示例3: GetRoles
public async Task<List<string>> GetRoles(string userId)
{
var db = new DataContext();
var userMan = new UserManager<MyUser>(new UserStore<MyUser>(db));
var roles = await userMan.GetRolesAsync(userId);
return roles.ToList();
}
示例4: GenerateUserIdentityAsync
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
var properties = this.GetType().GetProperties();
foreach (var property in properties)
{
if (property.GetValue(this) != null && property.Name != "PasswordHash")
userIdentity.AddClaim(new Claim(property.Name, property.GetValue(this).ToString()));
}
var roles = await manager.GetRolesAsync(Id);
foreach (var role in roles)
{
userIdentity.AddClaim(new Claim(ClaimTypes.Role, role));
}
return userIdentity;
}
示例5: DeleteConfirmed
public async Task<ActionResult> DeleteConfirmed(string id) {
var roleStore = new RoleStore<IdentityRole>(db);
var roleManager = new RoleManager<IdentityRole>(roleStore);
var userStore = new UserStore<ApplicationUser>(db);
var userManager = new UserManager<ApplicationUser>(userStore);
if (ModelState.IsValid) {
if (id == null) {
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var user = await userManager.FindByIdAsync(id);
// ev. 3parts inloggningar
var logins = user.Logins;
foreach (var login in logins.ToList()) {
await userManager.RemoveLoginAsync(login.UserId, new UserLoginInfo(login.LoginProvider, login.ProviderKey));
}
var rolesForUser = await userManager.GetRolesAsync(id);
if (rolesForUser.Count() > 0) {
foreach (var item in rolesForUser.ToList()) {
// item should be the name of the role
var result = await userManager.RemoveFromRoleAsync(user.Id, item);
}
}
if (user.Documents.Count() > 0) {
foreach (var doc in user.Documents.ToList()) {
db.Documents.Remove(doc);
}
}
await userManager.DeleteAsync(user);
return RedirectToAction("Index");
}
else {
return View();
}
}
示例6: Details
//
// GET: /Users/Details/5
public async Task<ActionResult> Details(string id)
{
ApplicationDbContext db = new ApplicationDbContext();
var userStore = new UserStore<ApplicationUser>(db);
var roleStore = new RoleStore<ApplicationRole>(db);
var userManager = new UserManager<ApplicationUser>(userStore);
var roleManager = new RoleManager<ApplicationRole>(roleStore);
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var user = await userManager.FindByIdAsync(id);
ViewBag.RoleNames = await userManager.GetRolesAsync(user.Id);
return View(user);
}