Exemplo n.º 1
0
        public async Task <IHttpActionResult> SetRolesInArea(iS3RolesInArea rolesInArea)
        {
            if (rolesInArea == null)
            {
                return(BadRequest("Argument Null"));
            }
            var user = await dbContext.Users.FirstOrDefaultAsync(c => c.UserName == rolesInArea.UserName);

            if (user == null)
            {
                return(BadRequest("User does not exists"));
            }

            var manager = new iS3UserManager(new UserStore <iS3IdentityUser>(dbContext));
            var claims  = await manager.GetClaimsAsync(user.Id);

            bool exist = claims.Any(c => c.Type == rolesInArea.AreaName);

            Claim          claim  = null;
            IdentityResult result = null;

            if (exist)
            {
                claim  = claims.FirstOrDefault(c => c.Type == rolesInArea.AreaName);
                result = await manager.RemoveClaimAsync(user.Id, claim);
            }
            claim  = new Claim(rolesInArea.AreaName, rolesInArea.Roles);
            result = await manager.AddClaimAsync(user.Id, claim);

            await dbContext.SaveChangesAsync();

            return(Ok("Roles in domain set"));
        }
Exemplo n.º 2
0
        //[Authorize(Roles = "Admin")]
        // Add a new user according to:
        //      UserName, Password, Role
        //
        public async Task <IHttpActionResult> AddUser(iS3LoginUser loginUser)
        {
            if (loginUser == null)
            {
                return(BadRequest("Argument Null"));
            }
            if (loginUser.Password != loginUser.ConfirmPassword)
            {
                return(BadRequest("Password not consistent"));
            }

            string password = loginUser.Password;

            // Erase the password for safety.
            loginUser.Password        = null;
            loginUser.ConfirmPassword = null;

            var userExists = await dbContext.Users.AnyAsync(c => c.UserName == loginUser.UserName);

            if (userExists)
            {
                //var exist = await dbContext.Users.FirstAsync(c => c.UserName == user.UserName);
                return(BadRequest("User already exists"));
            }

            var manager = new iS3UserManager(new UserStore <iS3IdentityUser>(dbContext));

            var user = new iS3IdentityUser(loginUser.UserName);

            var result = await manager.CreateAsync(user, password);

            if (!result.Succeeded)
            {
                return(BadRequest(result.Errors.FirstOrDefault()));
            }

            await manager.AddClaimAsync(user.Id,
                                        new Claim(ClaimTypes.Name, loginUser.UserName));

            await manager.AddClaimAsync(user.Id,
                                        new Claim(ClaimTypes.Role, loginUser.Role));

            // add a claim to Identity.Claims
            //   Claim.Type = iS3ClaimTypes.AuthorizedProjects,
            //   Claim.Value = user.AuthorizedProjects
            //
            //await manager.AddClaimAsync(user.Id,
            //    new Claim(iS3ClaimTypes.AuthorizedProjects, loginUser.AuthorizedProjects));

            await dbContext.SaveChangesAsync();

            string success = string.Format("User {0} created successfully.", loginUser.UserName);

            return(Ok(success));
        }
Exemplo n.º 3
0
        // Seed a default user: Admin
        //   Username=Admin, Password=iS3Admin, Role=Admin
        //
        // You should change it to your desired name and password.
        //
        protected async override void Seed(iS3OAuthDbContext context)
        {
            // Set up initial user: admin
            var admin = new iS3IdentityUser("Admin");

            // Introducing...the UserManager:
            var manager = new iS3UserManager(
                new UserStore <iS3IdentityUser>(context));

            var result = await manager.CreateAsync(admin, "iS3Admin");

            // Add claims for Admin
            await manager.AddClaimAsync(admin.Id,
                                        new Claim(ClaimTypes.Name, "Admin"));

            await manager.AddClaimAsync(admin.Id,
                                        new Claim(ClaimTypes.Role, "Admin"));

            context.SaveChanges();
        }