Example #1
0
        public ActionResult CreateRole(Role role)
        {
            if (ModelState.IsValid)
            {
                string statusMsg = string.Empty;
                using (AuthenticationDb db = new AuthenticationDb())
                {
                    var ro = db.Roles.FirstOrDefault(x => x.RoleName == role.RoleName);
                    if (ro == null)
                    {
                        var newRole = new Role
                        {
                            RoleName = role.RoleName
                        };

                        db.Roles.Add(newRole);
                        db.SaveChanges();
                        statusMsg = "Role Created Successfully!";
                    }
                    else
                    {
                        statusMsg = "Role is already exiests.";
                    }
                }
                ViewBag.msg = statusMsg;
            }
            return(View());
        }
        public ActionResult ActiveUserAccount(string otpCode, string username)
        {
            bool statusAccount = false;

            using (AuthenticationDb dbContext = new AuthenticationDb())
            {
                var userAccount = dbContext.Users.Where(u => u.Username.ToString().Equals(username)).FirstOrDefault();

                if (userAccount != null)
                {
                    if (userAccount.ActivationOtp == otpCode)
                    {
                        userAccount.IsActive = true;
                        dbContext.SaveChanges();
                        statusAccount = true;
                    }
                    else
                    {
                        ViewBag.Message = "Something Wrong !!";
                        return(RedirectToAction("Registration", "Account"));
                    }
                }
                else
                {
                    ViewBag.Message = "Something Wrong !!";
                    return(RedirectToAction("Registration", "Account"));
                }
            }
            ViewBag.Status = statusAccount;
            return(View());
        }
 public ActionResult ActiveAccount()
 {
     using (AuthenticationDb db = new AuthenticationDb())
     {
         var users = db.Users.Where(x => x.IsActive == false).ToList();
         return(View(users));
     }
 }
Example #4
0
 public ActionResult AssignRole()
 {
     using (AuthenticationDb db = new AuthenticationDb())
     {
         ViewBag.UserList = db.Users.ToList();
         ViewBag.RoleList = db.Roles.ToList();
     }
     return(View());
 }
        public ActionResult Registration(RegistrationVM rvm)
        {
            bool   regStatus           = false;
            string messageRegistration = string.Empty;

            if (ModelState.IsValid)
            {
                string email         = Membership.GetUserNameByEmail(rvm.Email);
                var    userName      = Membership.GetUser(rvm.Username, false);
                Guid   vcode         = Guid.NewGuid();
                Random random        = new Random();
                int    activationOtp = random.Next(100000, 999999);
                if (userName != null)
                {
                    ModelState.AddModelError("", "Sorry! Username already Exists! Try different one.");
                    return(View(rvm));
                }
                if (!string.IsNullOrEmpty(email))
                {
                    ModelState.AddModelError("", "Sorry! Email already Exists");
                    return(View(rvm));
                }

                using (AuthenticationDb db = new AuthenticationDb())
                {
                    var user = new User
                    {
                        Username       = rvm.Username,
                        FirstName      = rvm.FirstName,
                        LastName       = rvm.LastName,
                        Email          = rvm.Email,
                        Password       = rvm.Password,
                        ActivationCode = vcode,
                        ActivationOtp  = activationOtp.ToString()
                    };

                    db.Users.Add(user);
                    db.SaveChanges();
                    var ro = db.Roles.FirstOrDefault(x => x.RoleName == "User");
                    db.Users.Include("Roles").FirstOrDefault(x => x.UserId == user.UserId).Roles.Add(ro);
                    db.SaveChanges();
                }
                //VerificationEmail(rvm.Email, vcode.ToString(), activationOtp.ToString());
                /*if you uncomment avobe line then please add a valid gmail address & password to line 148 & 151 respectively*/
                messageRegistration = "Your account has been created successfully.";
                regStatus           = true;
            }
            else
            {
                messageRegistration = "Something Wrong!";
            }
            ViewBag.Message  = messageRegistration;
            ViewBag.Status   = regStatus;
            ViewBag.username = rvm.Username;
            return(View(rvm));
        }
Example #6
0
 public Operations(AuthenticationDb context)
 {
     if (context == null)
     {
         _db = new AuthenticationDb();
     }
     else
     {
         _db = context;
     }
     _userManager = new UserManager <IdentityUser>(new UserStore <IdentityUser>(_db));
     _roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(_db));
 }
 public ActionResult DeactiveAccount()
 {
     using (AuthenticationDb db = new AuthenticationDb())
     {
         var users  = db.Roles.Where(x => x.RoleName == "Admin").SelectMany(x => x.Users).ToList();
         var acUser = db.Users.Where(x => x.IsActive == true).ToList();
         foreach (var item in users)
         {
             acUser.Remove(item);
         }
         return(View(acUser));
     }
 }
 public ActionResult ActiveAccount(string Username)
 {
     using (AuthenticationDb db = new AuthenticationDb())
     {
         var user = db.Users.FirstOrDefault(x => x.Username == Username);
         if (user != null)
         {
             user.IsActive        = true;
             db.Entry(user).State = EntityState.Modified;
             db.SaveChanges();
             var usersn = db.Users.Where(x => x.IsActive == false).ToList();
             return(View(usersn));
         }
         var users = db.Users.Where(x => x.IsActive == false).ToList();
         return(View(users));
     }
 }
 public ActionResult DeactiveAccount(string username)
 {
     using (AuthenticationDb db = new AuthenticationDb())
     {
         var user = db.Users.FirstOrDefault(x => x.Username == username);
         if (user != null)
         {
             user.IsActive        = false;
             db.Entry(user).State = EntityState.Modified;
             db.SaveChanges();
         }
         var users  = db.Roles.Where(x => x.RoleName == "Admin").SelectMany(x => x.Users).ToList();
         var acUser = db.Users.Where(x => x.IsActive == true).ToList();
         foreach (var item in users)
         {
             acUser.Remove(item);
         }
         return(View(acUser));
     }
 }
Example #10
0
        public ActionResult AssignRole(UserRolesVM uvm)
        {
            string msg = string.Empty;

            if (ModelState.IsValid)
            {
                using (AuthenticationDb db = new AuthenticationDb())
                {
                    var us = db.Users.Include("Roles").FirstOrDefault(x => x.UserId == uvm.UserId);
                    var ro = db.Roles.FirstOrDefault(x => x.RoleId == uvm.RoleId);


                    db.Users.Include("Roles").FirstOrDefault(x => x.UserId == uvm.UserId).Roles.Add(ro);

                    db.SaveChanges();
                    ViewBag.UserList = db.Users.ToList();
                    ViewBag.RoleList = db.Roles.ToList();
                }
            }
            return(View());
        }