当前位置: 首页>>代码示例>>C#>>正文


C# UserManager.GetRolesAsync方法代码示例

本文整理汇总了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();
      }
开发者ID:TheGrandUser,项目名称:Hexcrawl,代码行数:23,代码来源:HomeController.cs

示例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);
        }
开发者ID:dmacan23,项目名称:FireBreathingRubberDuckies---Sensei,代码行数:30,代码来源:WebApiHandler.cs

示例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();
        }
开发者ID:biggash730,项目名称:SemanticUI_Knockout_ASPMVC_Starter,代码行数:8,代码来源:SecurityHelpers.cs

示例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;
        }
开发者ID:james-russo,项目名称:Web-Mobile-Template,代码行数:21,代码来源:IdentityModels.cs

示例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();
            }
        }
开发者ID:StaffanE,项目名称:LexiconLMS,代码行数:44,代码来源:UsersController.cs

示例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);
        }
开发者ID:leaswis,项目名称:TempPossibleTrash,代码行数:20,代码来源:UserProfileController.cs


注:本文中的UserManager.GetRolesAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。