예제 #1
0
        public static ApplicationUserClaimRepository GetApplicationUserClaimRepository(IUnitOfWork unitOfWork)
        {
            var repository = new ApplicationUserClaimRepository();

            repository.UnitOfWork = unitOfWork;
            return(repository);
        }
예제 #2
0
        public async Task <ActionResult> Register(RegisterViewModel register, string ReturnUrl = null)
        {
            ApplicationUserRepository      userRepo;
            ApplicationUserClaimRepository userClaimRepo;
            ApplicationUser user;
            //ApplicationUserClaim userClaim;
            //AuthenticationProperties props = null;
            const string issuer = "Local Authority";
            int          id     = 0;

            if (ModelState.IsValid)
            {
                try
                {
                    userRepo      = new ApplicationUserRepository(settings, logger, dbc);
                    userClaimRepo = new ApplicationUserClaimRepository(settings, logger, dbc);
                    user          = userRepo.FindAll().FirstOrDefault(u => u.NormalizedUserName == register.UserName.ToUpper());

                    if (user == null)
                    {
                        user = new ApplicationUser()
                        {
                            UserName           = register.UserName,
                            NormalizedUserName = register.UserName.ToUpper(),
                            Email                = register.Email,
                            NormalizedEmail      = register.Email.ToUpper(),
                            EmailConfirmed       = true,
                            PhoneNumber          = String.Empty,
                            PhoneNumberConfirmed = false,
                            TwoFactorEnabled     = false,
                            DOB        = DateTime.Now,
                            Department = String.Empty,
                            Active     = true, ModifiedDt = DateTime.Now, CreateDt = DateTime.Now,
                            Claims     = new List <ApplicationUserClaim>()
                        };

                        // NOTE: This should be wrapped in a Unit of Work
                        // Add User to the database
                        user.PasswordHash = PasswordHash.HashPassword(register.Password);
                        id = (int)userRepo.Add(user);
                        logger.LogInformation($"Created new user account: {register.UserName}");

                        // Add default User Claims
                        user.Claims.Add(new ApplicationUserClaim()
                        {
                            UserId    = id,
                            ClaimType = ClaimTypes.Name, ClaimValue = user.UserName, ClaimIssuer = issuer,
                            Active    = true, ModifiedDt = DateTime.Now, CreateDt = DateTime.Now
                        });
                        user.Claims.Add(new ApplicationUserClaim()
                        {
                            UserId    = id,
                            ClaimType = ClaimTypes.NameIdentifier, ClaimValue = id.ToString(), ClaimIssuer = issuer,
                            Active    = true, ModifiedDt = DateTime.Now, CreateDt = DateTime.Now
                        });
                        user.Claims.Add(new ApplicationUserClaim()
                        {
                            UserId    = id,
                            ClaimType = ClaimTypes.Role, ClaimValue = "Basic", ClaimIssuer = issuer,
                            Active    = true, ModifiedDt = DateTime.Now, CreateDt = DateTime.Now
                        });
                        user.Claims.Add(new ApplicationUserClaim()
                        {
                            UserId    = id,
                            ClaimType = ClaimTypes.DateOfBirth, ClaimValue = user.DOB.ToString("yyyy-MM-dd hh:mm:ss"), ClaimIssuer = issuer,
                            Active    = true, ModifiedDt = DateTime.Now, CreateDt = DateTime.Now
                        });

                        // Add User Claims to the database
                        foreach (ApplicationUserClaim c in user.Claims)
                        {
                            userClaimRepo.Add(c);
                        }

                        logger.LogInformation($"Assigned default claims to new user account: {register.UserName}");

                        // Sign in the user
                        await identityManager.SignInAsync(user);

                        logger.LogInformation($"Logged in user: {user.UserName} at IP address: {HttpContext.Connection.RemoteIpAddress}");

                        if (ReturnUrl == null)
                        {
                            return(LocalRedirect("/Home/LoginSuccess"));
                        }
                        else
                        {
                            return(LocalRedirect($"{ReturnUrl}"));
                        }
                    }
                    else
                    {
                        logger.LogError($"User is already registered: {register.UserName}");
                        return(LocalRedirect("/Account/UserAlreadyRegistered"));
                    }
                }
                catch (Exception ex)
                {
                    logger.LogError($"Exception registering user({register.UserName}): {ex.Message}");
                    return(RedirectToAction("Error"));
                }
            }

            return(RedirectToAction("Error"));
        }
예제 #3
0
        public async Task <IActionResult> Login(LoginViewModel login, string ReturnUrl = null)
        {
            ApplicationUserRepository      userRepo      = null;
            ApplicationUserClaimRepository userClaimRepo = null;
            ApplicationAuditLogRepository  logRepo       = null;
            ApplicationUser user = null;

            try
            {
                if (ModelState.IsValid)
                {
                    userRepo      = new ApplicationUserRepository(settings, logger, dbc);
                    userClaimRepo = new ApplicationUserClaimRepository(settings, logger, dbc);
                    logRepo       = new ApplicationAuditLogRepository(settings, logger, dbc);

                    // Find user in database and validate there password
                    user = (userRepo.FindAll()).FirstOrDefault(u => u.NormalizedUserName == login.UserName.ToUpper());
                    if (user != null)
                    {
                        //valid = PasswordHash.ValidateHashedPassword(user.PasswordHash, login.Password);

                        // Add user claims from the database
                        user.Claims = new List <ApplicationUserClaim>();
                        foreach (ApplicationUserClaim c in userClaimRepo.FindAll().Where(c => c.UserId == user.Id))
                        {
                            user.Claims.Add(c);
                        }

                        // Sign in the user, if there password is correct
                        if (await identityManager.SignInWithPasswordAsync(user, login.Password))
                        {
                            logRepo.Add(new ApplicationAuditLog()
                            {
                                CategoryId = 1, Description = $"User ({user.UserName}) logged into application at IP address: {HttpContext.Connection.RemoteIpAddress}.", UserName = user.UserName
                            });
                            logger.LogInformation($"Logged in user: {login.UserName} at IP address: {HttpContext.Connection.RemoteIpAddress}");
                            if (ReturnUrl == null)
                            {
                                return(LocalRedirect("/Home/LoginSuccess"));
                            }
                            else
                            {
                                return(LocalRedirect($"{ReturnUrl}"));
                            }
                        }
                        else
                        {
                            logger.LogError($"Invalid user name / password combination: {login.UserName} at IP address {HttpContext.Connection.RemoteIpAddress}");
                            logRepo.Add(new ApplicationAuditLog()
                            {
                                CategoryId = 1, Description = $"Invalid user name / password combination for User ({user.UserName}) at IP address {HttpContext.Connection.RemoteIpAddress}.", UserName = user.UserName
                            });
                            return(LocalRedirect("/Home/InvalidCredentials"));
                        }
                    }
                    else
                    {
                        logger.LogError($"Invalid user name / password combination: {login.UserName} at IP address: {HttpContext.Connection.RemoteIpAddress}");
                        return(LocalRedirect("/Home/InvalidCredentials"));
                    }
                }
                else
                {
                    logger.LogError("Invalid ModelState: AccountController.Login()");
                    return(View($"LoginRegister?action=login?ReturnUrl={ReturnUrl}"));
                }
            }
            catch (Exception ex)
            {
                logger.LogError($"Exception: {ex.Message}");
            }

            return(RedirectToAction("Error"));
        }
예제 #4
0
        public async Task <ActionResult> EditUser(ApplicationUserViewModel view)
        {
            ApplicationUserRepository      userRepo;
            ApplicationRoleRepository      roleRepo;
            ApplicationUserClaimRepository userClaimRepo;
            ApplicationAuditLogRepository  logRepo;
            ApplicationUserClaim           userClaim = null;
            IList <string> currentRoles         = null;
            string         role                 = String.Empty;
            int            claimId              = 0;
            int            rows                 = 0;
            const string   issuer               = "Local Authority";
            const string   claimTypesDepartment = "Department";
            bool           isCurrentUser        = view.User.UserName.ToUpper() == HttpContext.User.Identity.Name.ToUpper() ? true : false;

            try
            {
                userRepo      = new ApplicationUserRepository(settings, logger, dbc);
                userClaimRepo = new ApplicationUserClaimRepository(settings, logger, dbc);
                roleRepo      = new ApplicationRoleRepository(settings, logger, dbc);
                logRepo       = new ApplicationAuditLogRepository(settings, logger, dbc);

                if (ModelState.IsValid)
                {
                    // Update the user in the database
                    userRepo.Update(view.User);

                    //Add DOB claim to database and to claims list of the current user, if applicable
                    userClaim = (userClaimRepo.FindAll()).FirstOrDefault(uc => uc.UserId == view.User.Id && uc.ClaimType == ClaimTypes.DateOfBirth);
                    if (userClaim != null)
                    {
                        if (userClaim.ClaimValue != view.User.DOB.ToString("yyyy-MM-dd hh:mm:ss"))
                        {
                            userClaim.ModifiedDt = DateTime.Now;
                            userClaim.ClaimValue = view.User.DOB.ToString("yyyy-MM-dd hh:mm:ss");
                            // Update the database
                            userClaimRepo.Update(userClaim);
                            logger.LogInformation($"Updated claim({ClaimTypes.DateOfBirth}) for user account: {view.User.UserName}");
                        }
                        else
                        {
                            // Nothing changed, so no need to update the database
                        }
                    }
                    else
                    {
                        // Add DOB claim to the database
                        userClaim = new ApplicationUserClaim()
                        {
                            UserId    = view.User.Id,
                            ClaimType = ClaimTypes.DateOfBirth, ClaimValue = view.User.DOB.ToString("yyyy-MM-dd hh:mm:ss"), ClaimIssuer = issuer,
                            Active    = true, ModifiedDt = DateTime.Now, CreateDt = DateTime.Now
                        };
                        userClaimRepo.Add(userClaim);
                        logger.LogInformation($"Added new claim({ClaimTypes.DateOfBirth}) to user account: {view.User.UserName}");
                    }

                    //TODO: Department still has issues when transitioning from null to not null
                    //Add Department claim to database and to claims list of the user
                    userClaim = (userClaimRepo.FindAll()).FirstOrDefault(uc => uc.UserId == view.User.Id && uc.ClaimType == claimTypesDepartment);
                    if (userClaim != null)
                    {
                        if (view.User.Department != null && userClaim.ClaimValue != view.User.Department)
                        {
                            userClaim.ModifiedDt = DateTime.Now;
                            userClaim.ClaimValue = view.User.Department;
                            userClaimRepo.Update(userClaim);
                            logger.LogInformation($"Updated claim({claimTypesDepartment}) for user account: {view.User.UserName}");
                        }
                        else
                        {
                            // Nothing changed, so no need to update the database
                        }
                    }
                    else
                    {
                        if (view.User.Department != null)
                        {
                            userClaim = new ApplicationUserClaim()
                            {
                                UserId    = view.User.Id,
                                ClaimType = claimTypesDepartment, ClaimValue = view.User.Department, ClaimIssuer = issuer,
                                Active    = true, ModifiedDt = DateTime.Now, CreateDt = DateTime.Now
                            };
                            userClaimRepo.Add(userClaim);
                            logger.LogInformation($"Assigned new claim({claimTypesDepartment}) to user account: {view.User.UserName}");
                        }
                    }

                    //Add Role claim to database and to claims list of the user
                    // Process the roles and update the role store
                    currentRoles = (userClaimRepo.FindAll()).Where(uc => uc.UserId == view.User.Id && uc.ClaimType == ClaimTypes.Role).Select(r => r.ClaimValue).ToList();;
                    if (view.User.RoleBadges != null)
                    {
                        foreach (string r in view.User.RoleBadges.Split("|"))
                        {
                            if (r != String.Empty)
                            {
                                role = r.Substring(r.IndexOf('-') + 1, r.Length - r.IndexOf('-') - 1);
                                // Add, if it's a new role
                                if (!currentRoles.Contains(role))
                                {
                                    claimId = (int)userClaimRepo.Add(new ApplicationUserClaim()
                                    {
                                        UserId    = view.User.Id,
                                        ClaimType = ClaimTypes.Role, ClaimValue = role, ClaimIssuer = issuer,
                                        Active    = true, ModifiedDt = DateTime.Now, CreateDt = DateTime.Now
                                    });

                                    if (claimId > 0)
                                    {
                                        logger.LogInformation($"Assigned role({role}) to user account: {view.User.UserName}");
                                    }
                                    else
                                    {
                                        logger.LogError($"Error assigning role({role}) to user account: {view.User.UserName}");
                                    }
                                }
                            }
                        }
                    }
                    // Remove any roles of which the user is no longer a member
                    foreach (string r in currentRoles)
                    {
                        if (!view.User.RoleBadges.Contains(r))
                        {
                            claimId = (userClaimRepo.FindAll()).FirstOrDefault(c => c.ClaimType == ClaimTypes.Role && c.UserId == view.User.Id).Id;
                            rows    = userClaimRepo.Delete(new PrimaryKey()
                            {
                                Key = (int)claimId, IsIdentity = true
                            });
                            if (rows > 0)
                            {
                                logger.LogInformation($"Removed role({r}) from user account: {view.User.UserName}");
                            }
                            else
                            {
                                logger.LogError($"Error removing role({r}) from account: {view.User.UserName}");
                            }
                        }
                    }

                    // If we've updated the claims for the currently signed-in user,
                    // then refresh Cookie by recreating the User Security Principal from the database
                    if (isCurrentUser)
                    {
                        await identityManager.RefreshClaimsAsync(view.User, userClaimRepo.FindAll().Where(uc => uc.UserId == view.User.Id).ToList());

                        logRepo.Add(new ApplicationAuditLog()
                        {
                            CategoryId = 1, Description = $"User ({view.User.UserName}) logged into application with refreshed claims."
                        });
                        logger.LogInformation($"Refreshed cookie for user: {view.User.UserName}");
                    }

                    return(RedirectToAction("Index", "Account"));
                }
                else
                {
                    userClaimRepo = new ApplicationUserClaimRepository(settings, logger, dbc);
                    userRepo      = new ApplicationUserRepository(settings, logger, dbc);

                    view.User = userRepo.FindByPKView(new PrimaryKey()
                    {
                        Key = view.User.Id, IsIdentity = true
                    });
                    view.Roles = roleRepo.FindAll();

                    // Update the RoleBadges
                    foreach (ApplicationUserClaim uc in view.User.Claims.Where(uc => uc.UserId == view.User.Id))
                    {
                        view.User.RoleBadges += String.Format("{0}-{1}|", uc.Id, uc.ClaimType);
                    }

                    return(View("EditUser", view));
                }
            }
            catch (Exception ex)
            {
                throw (Exception)Activator.CreateInstance(ex.GetType(), ex.Message + ex.StackTrace);
            }
        }
예제 #5
0
        public async Task <ActionResult> EditProfile(ApplicationUserViewModel view)
        {
            ApplicationUserRepository      userRepo;
            ApplicationUserClaimRepository userClaimRepo;
            ApplicationAuditLogRepository  logRepo;
            ApplicationUserClaim           userClaim = null;
            ClaimsPrincipal userPrincipal = null;
            Claim           dobClaim = null; Claim deptClaim = null;
            const string    issuer = "Local Authority";
            const string    claimTypesDepartment = "Department";

            try
            {
                userRepo      = new ApplicationUserRepository(settings, logger, dbc);
                userClaimRepo = new ApplicationUserClaimRepository(settings, logger, dbc);
                logRepo       = new ApplicationAuditLogRepository(settings, logger, dbc);

                if (ModelState.IsValid)
                {
                    userRepo.Update(view.User);

                    //Add DOB claim to database and to claims list of the user
                    dobClaim = HttpContext.User.FindFirst(ClaimTypes.DateOfBirth);
                    if (dobClaim != null)
                    {
                        ((ClaimsIdentity)HttpContext.User.Identity).RemoveClaim(dobClaim);
                        userClaim            = (userClaimRepo.FindAll()).FirstOrDefault(uc => uc.UserId == view.User.Id && uc.ClaimType == ClaimTypes.DateOfBirth);
                        userClaim.ModifiedDt = DateTime.Now;
                        userClaim.ClaimValue = view.User.DOB.ToString("yyyy-MM-dd hh:mm:ss");
                        userClaimRepo.Update(userClaim);
                    }
                    else
                    {
                        userClaimRepo.Add(new ApplicationUserClaim()
                        {
                            UserId      = view.User.Id,
                            ClaimType   = ClaimTypes.DateOfBirth,
                            ClaimValue  = view.User.DOB.ToString("yyyy-MM-dd hh:mm:ss"),
                            ClaimIssuer = issuer,
                            Active      = true,
                            ModifiedDt  = DateTime.Now,
                            CreateDt    = DateTime.Now
                        });
                    }

                    ((ClaimsIdentity)HttpContext.User.Identity)
                    .AddClaim(new Claim(ClaimTypes.DateOfBirth, view.User.DOB.ToString("yyyy-MM-dd hh:mm:ss"), ClaimValueTypes.String, issuer));

                    //Add Department claim to database and to claims list of the user
                    if (view.User.Department != null)
                    {
                        deptClaim = HttpContext.User.FindFirst(claimTypesDepartment);
                        if (deptClaim != null)
                        {
                            ((ClaimsIdentity)HttpContext.User.Identity).RemoveClaim(deptClaim);
                            userClaim            = (userClaimRepo.FindAll()).FirstOrDefault(uc => uc.UserId == view.User.Id && uc.ClaimType == claimTypesDepartment);
                            userClaim.ModifiedDt = DateTime.Now;
                            userClaim.ClaimValue = view.User.Department;
                            userClaimRepo.Update(userClaim);
                        }
                        else
                        {
                            userClaimRepo.Add(new ApplicationUserClaim()
                            {
                                UserId      = view.User.Id,
                                ClaimType   = claimTypesDepartment,
                                ClaimValue  = view.User.Department,
                                ClaimIssuer = issuer,
                                Active      = true,
                                ModifiedDt  = DateTime.Now,
                                CreateDt    = DateTime.Now
                            });
                        }
                        ((ClaimsIdentity)HttpContext.User.Identity)
                        .AddClaim(new Claim(claimTypesDepartment, view.User.Department, ClaimValueTypes.String, issuer));
                    }

                    // We've updated the claims for the currently signed-in user,
                    // So refresh Cookie by recreating the User Security Principal from the database
                    await identityManager.RefreshClaimsAsync(view.User, userClaimRepo.FindAll().Where(uc => uc.UserId == view.User.Id).ToList());

                    logRepo.Add(new ApplicationAuditLog()
                    {
                        CategoryId = 1, Description = $"User ({view.User.UserName}) logged into application with refreshed claims."
                    });

                    // Refresh Cookie by recreating the User Security Principal from the current Identity Principal
                    //userPrincipal = new ClaimsPrincipal(HttpContext.User.Identity);

                    // Sign In User
                    //await HttpContext.SignInAsync(
                    //	CookieAuthenticationDefaults.AuthenticationScheme,
                    //	userPrincipal,
                    //	new AuthenticationProperties()
                    //	{
                    //		IsPersistent = false
                    //	});

                    logger.LogInformation($"Refreshed cookie for user: {view.User.UserName}");

                    return(RedirectToAction("LoginSuccess", "Home"));
                }
                else
                {
                    userClaimRepo = new ApplicationUserClaimRepository(settings, logger, dbc);
                    userRepo      = new ApplicationUserRepository(settings, logger, dbc);

                    view.User = userRepo.FindByPKView(new PrimaryKey()
                    {
                        Key = view.User.Id, IsIdentity = true
                    });
                    //view.Roles = ApplicationRole.Roles;

                    return(View("EditProfile", view));
                }
            }
            catch (Exception ex)
            {
                throw (Exception)Activator.CreateInstance(ex.GetType(), ex.Message + ex.StackTrace);
            }
        }