Exemple #1
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));
        }
Exemple #2
0
        public override async Task GrantResourceOwnerCredentials(
            OAuthGrantResourceOwnerCredentialsContext context)
        {
            // ** Use extension method to get a reference to the user manager from the Owin Context:
            var manager = context.OwinContext.GetUserManager <iS3UserManager>();
            var user    = await manager.FindAsync(context.UserName, context.Password);

            if (user == null)
            {
                if (context.UserName == "Admin")
                {
                    var admin  = new iS3IdentityUser("Admin");
                    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"));

                    user = await manager.FindAsync(context.UserName, context.Password);

                    await context.OwinContext.Get <iS3OAuthDbContext>().SaveChangesAsync();
                }
                else
                {
                    context.SetError(
                        "invalid_grant", "The user name or password is incorrect.");
                    context.Rejected();
                    return;
                }
            }
            // UserManager allows us to retrieve use with name/password combo:
            // Add claims associated with this user to the ClaimsIdentity object:
            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            foreach (var userClaim in user.Claims)
            {
                identity.AddClaim(new Claim(userClaim.ClaimType, userClaim.ClaimValue));
            }

            context.Validated(identity);
        }
Exemple #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();
        }