Example #1
31
      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();
      }
        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);
        }
        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();
        }
        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;
        }
Example #5
0
        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();
            }
        }
        //
        // 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);
        }