Example #1
0
        public IHttpActionResult DeleteUser(string id)
        {
            //check if such a user exists in the database
            var userToDelete = new MyUserManager().GetUserDetailById(id); //this.UserManager.FindById(id);

            if (userToDelete == null)
            {
                return(this.NotFound());
            }
            else if (userToDelete.IsDeleted ?? false)
            {
                return(this.BadRequest("User already deleted"));
            }
            else
            {
                try
                {
                    if (!new MyUserManager().DeleteUser(userToDelete))
                    {
                        return(this.BadRequest("Unable to  delete user. Please contact administrator"));
                    }
                }
                catch (Exception ex) { return(this.BadRequest(ex.Message)); }
            }
            return(this.Ok());
        }
Example #2
0
        public async Task <IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = new ApplicationUser()
            {
                UserName = model.Email, Email = model.Email
            };

            //set the IsDeleted property to false
            user.IsDeleted = false;
            UserModal ouser = new UserModal();

            ouser.Id           = user.Id;
            ouser.Email        = user.Email;
            ouser.UserName     = user.UserName;
            ouser.PasswordHash = MyEncryption.Encrypt(model.Password);
            bool flag = false;

            try
            {
                flag = new MyUserManager().CreateUser(ouser); //await UserManager.CreateAsync(user, model.Password);
            }
            catch (Exception ex) { ModelState.AddModelError("", ex.Message); }
            if (!flag)
            {
                ModelState.AddModelError("", "Failed to create user");
                return(BadRequest(ModelState));
            }

            return(Ok());
        }
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

            MyUser user = new MyAccountDataAccess().GetUserDetailByUserName(context.UserName);

            if (user != null)
            {
                if (user.UserName == context.UserName && user.PasswordHash == MyEncryption.Encrypt(context.Password))
                {
                    foreach (var r in user.Roles)
                    {
                        identity.AddClaim(new Claim(ClaimTypes.Role, r.RoleName));
                    }
                    identity.AddClaim(new Claim(ClaimTypes.Sid, user.Id));
                    identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
                    identity.AddClaim(new Claim(ClaimTypes.Email, user.Email));

                    var props = new AuthenticationProperties(new Dictionary <string, string>
                    {
                        {
                            "userdisplayname", context.UserName
                        },
                        {
                            "Email", user.Email
                        }
                    });

                    var ticket = new AuthenticationTicket(identity, props);
                    context.Validated(ticket);
                }
                else
                {
                    context.SetError("invalid_grant", "Provided username and password is incorrect");
                    context.Rejected();
                }
            }
            else
            {
                context.SetError("invalid_grant", "Provided username and password is incorrect");
                context.Rejected();
            }
            return;
        }
Example #4
0
        public async Task <IHttpActionResult> SetPassword(SetPasswordBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            CurrentLoginData currentLogin = CurrentLoginData.FromIdentity(User.Identity as ClaimsIdentity);

            if (currentLogin == null)
            {
                return(NotFound());
            }

            if (model.NewPassword != model.ConfirmPassword)
            {
                ModelState.AddModelError("", "New and confirm password should be same.");
                return(BadRequest(ModelState));
            }
            else
            {
                MyUser ouser = new MyUser();
                ouser.Id           = currentLogin.Id;
                ouser.Email        = currentLogin.Email;
                ouser.PasswordHash = MyEncryption.Encrypt(model.NewPassword);

                bool flag = false;
                try
                {
                    flag = new MyUserManager().ChangeUserPassword(ouser); //await UserManager.CreateAsync(user, model.Password);
                }
                catch (Exception ex) { ModelState.AddModelError("", ex.Message); }
                if (!flag)
                {
                    ModelState.AddModelError("", "Failed to set user password");
                    return(BadRequest(ModelState));
                }
            }
            return(Ok());
        }
Example #5
0
        public async Task <IHttpActionResult> RemoveRolesFromUser(string id, string[] rolesToRemove)
        {
            if (rolesToRemove == null)
            {
                return(this.BadRequest("No roles specified"));
            }

            ///find the user we want to assign roles to
            MyUser appUser = new MyUserManager().GetUserDetailById(id); //await this.UserManager.FindByIdAsync(id);

            if (appUser == null || (appUser.IsDeleted ?? false))
            {
                return(NotFound());
            }

            ///check if the user currently has any roles
            List <MyUserRoles> currentRoles = appUser.Roles;                     // await this.UserManager.GetRolesAsync(appUser.Id);
            List <MyRole>      allRoles     = new MyUserManager().GetAllRoles(); // await this.UserManager.GetRolesAsync(appUser.Id);

            if (allRoles == null || allRoles.Count == 0)
            {
                return(NotFound());
            }


            List <MyRole> tempRoles = allRoles.Where(n => currentRoles.Any(x => x.RoleName.Contains(n.RoleName))).Select(x => new MyRole {
                RoleId = x.RoleId, RoleName = x.RoleName
            }).ToList();

            List <MyRole> FinalrolesToRemove = tempRoles.Where(n => rolesToRemove.Contains(n.RoleName)).Select(x => new MyRole {
                RoleId = x.RoleId, RoleName = x.RoleName
            }).ToList();

            if (FinalrolesToRemove.Count() == 0)
            {
                ModelState.AddModelError("", string.Format("Roles '{0}' does not exist in the system", string.Join(",", FinalrolesToRemove)));
                return(this.BadRequest(ModelState));
            }

            ///remove user from current roles, if any
            // bool flag = new MyUserManager().RemoveUserRoles (appUser.Id, currentRole  );
            string strRoles = string.Empty;

            foreach (MyRole r in FinalrolesToRemove)
            {
                try
                {  ///remove role from user
                    if (!new MyUserManager().RemoveUserRoles(appUser.Id, r.RoleId))
                    {
                        strRoles += r.RoleName + ", ";
                    }
                }
                catch
                {
                    strRoles += r.RoleName + ", ";
                }
            }
            if (!string.IsNullOrEmpty(strRoles))
            {
                ModelState.AddModelError("", "Failed to remove " + strRoles + " user roles");
                return(BadRequest(ModelState));
            }

            return(Ok(new { userId = id, rolesRemoved = FinalrolesToRemove.Select(x => x.RoleName).ToArray() }));
        }