Exemplo n.º 1
0
        public static List<ref_States> GetStates()
        {
            ObjectCache cache = MemoryCache.Default;

            List<ref_States> states = (List<ref_States>)cache[CacheKey.States];

            if (states == null)
            {
                CacheItemPolicy policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = DateTime.Now.AddHours(double.Parse(Config.cacheLength));

                using (WW_DevEntities context = new WW_DevEntities())
                {
                    states = context.ref_States.ToList();
                }

                cache.Set(CacheKey.States, states, policy);
            }

            return states;
        }
Exemplo n.º 2
0
 public DAL.RefreshToken FindRefreshToken(string hashedTokenId)
 {
     using (WW_DevEntities context = new WW_DevEntities())
     {
         return context.RefreshTokens.Where(a => a.Id == hashedTokenId).FirstOrDefault();
     }
 }
Exemplo n.º 3
0
        public async Task<bool> AddRefreshToken(DAL.RefreshToken token)
        {
            try
            {
                using (WW_DevEntities context = new WW_DevEntities())
                {
                    DAL.RefreshToken refreshToken = context.RefreshTokens.Where(a => a.Subject == token.Subject && a.ClientId == token.ClientId).FirstOrDefault();

                    if (refreshToken != null)
                        context.RefreshTokens.Remove(refreshToken);

                    context.RefreshTokens.Add(token);

                    return await context.SaveChangesAsync() > 0;
                }
            }
            catch (Exception ex)
            {
                ErroLogging.LogError(ex);
                throw ex;
            }
        }
Exemplo n.º 4
0
 public async Task<bool> RemoveRefreshToken(string hashedTokenId)
 {
     try
     {
         using (WW_DevEntities context = new WW_DevEntities())
         {
             DAL.RefreshToken refreshToken = context.RefreshTokens.Where(a => a.Id == hashedTokenId).FirstOrDefault();
             context.RefreshTokens.Remove(refreshToken);
             return await context.SaveChangesAsync() > 0;
         }
     }
     catch (Exception ex)
     {
         ErroLogging.LogError(ex);
         throw ex;
     }
 }
Exemplo n.º 5
0
        public async Task<bool> PasswordHistoryInsert(string AspNetUserId, string userPassword)
        {
            try
            {
                using (WW_DevEntities context = new WW_DevEntities())
                {
                    userPassword = Hash.Get(userPassword);

                    context.PasswordHistories.Add(new PasswordHistory
                    {
                        CreatedDate = DateTime.Now,
                        HashPassword = userPassword,
                        UserID = AspNetUserId

                    });

                    return await context.SaveChangesAsync() > 0;
                }
            }
            catch (Exception ex)
            {
                ErroLogging.LogError(ex);
                throw ex;
            }
        }
Exemplo n.º 6
0
        public async Task<IdentityResult> RegisterUser(UserModel userModel)
        {
            ApplicationUser user = new ApplicationUser();

            try
            {
                user = new ApplicationUser
                {
                    UserName = userModel.Email,
                    Email = userModel.Email,
                    TwoFactorEnabled = true//,
                    //PhoneNumber = userModel.MobilePhoneNumber
                };

                IdentityResult result = await UserManager.CreateAsync(user, userModel.Password);

                if (result.Succeeded)
                {
                    var roleresult = UserManager.AddToRole(user.Id, RoleType.NonClient);

                    //if (string.IsNullOrEmpty(userModel.SSN))
                    //    userModel.SSN = null;

                    using (WW_DevEntities context = new WW_DevEntities())
                    {
                        Client model = new Client
                        {
                            Id = Guid.NewGuid(),
                            AspUserId = user.Id,
                            FirstName = ProperCase.Convert(userModel.FirstName),
                            LastName = ProperCase.Convert(userModel.LastName),
                            MiddleName = ProperCase.Convert(userModel.MiddleName),
                            Suffix = ProperCase.Convert(userModel.Suffix),
                            //Gender = userModel.Gender,
                            //SSN = userModel.SSN,
                            //DateOfBirth = userModel.DateOfBirth,
                            CreatedDate = DateTime.Now
                        };

                        context.Clients.Add(model);

                        context.PasswordHistories.Add(new PasswordHistory
                        {
                            CreatedDate = DateTime.Now,
                            HashPassword = Hash.Get(userModel.Password),
                            UserID = user.Id
                        });

                        //ClientPhone mPhone = new ClientPhone();
                        //ClientPhone wPhone = new ClientPhone();
                        //ClientPhone hPhone = new ClientPhone();

                        //if (userModel.MobilePhoneNumber != null)
                        //{
                        //    mPhone.PhoneNumber = userModel.MobilePhoneNumber;
                        //    mPhone.CreatedDate = DateTime.Now;
                        //    context.ClientPhones.Add(mPhone);
                        //}

                        //if (userModel.WorkPhoneNumber != null)
                        //{
                        //    wPhone.PhoneNumber = userModel.WorkPhoneNumber;
                        //    wPhone.CreatedDate = DateTime.Now;
                        //    context.ClientPhones.Add(wPhone);
                        //}

                        //if (userModel.HomePhoneNumber != null)
                        //{
                        //    hPhone.PhoneNumber = userModel.HomePhoneNumber;
                        //    hPhone.CreatedDate = DateTime.Now;
                        //    context.ClientPhones.Add(hPhone);
                        //}

                        await context.SaveChangesAsync();

                        //if (userModel.MobilePhoneNumber != null)
                        //{

                        //    context.ClientPhoneAssociatives.Add(new ClientPhoneAssociative()
                        //    {
                        //        Id = model.Id,
                        //        ClientPhoneId = mPhone.Id,
                        //        PhoneTypeId = Guid.Parse(Config.MobilePhoneType),
                        //        CreatedDate = DateTime.Now
                        //    });
                        //}

                        //if (userModel.WorkPhoneNumber != null)
                        //{
                        //    context.ClientPhoneAssociatives.Add(new ClientPhoneAssociative()
                        //    {
                        //        Id = model.Id,
                        //        ClientPhoneId = wPhone.Id,
                        //        PhoneTypeId = Guid.Parse(Config.WorkPhoneType),
                        //        CreatedDate = DateTime.Now
                        //    });
                        //}

                        //if (userModel.HomePhoneNumber != null)
                        //{
                        //    context.ClientPhoneAssociatives.Add(new ClientPhoneAssociative()
                        //    {
                        //        Id = model.Id,
                        //        ClientPhoneId = hPhone.Id,
                        //        PhoneTypeId = Guid.Parse(Config.HomePhoneType),
                        //        CreatedDate = DateTime.Now
                        //    });
                        //}

                        //await context.SaveChangesAsync();

                        await SendConfirmationEmail(user.Id, EmailMessage.NewAccountEmailConfirmation);
                    }
                }

                return result;
            }
            catch (Exception ex)
            {
                //if error on creating address and phone need to remove login             
                UserManager.Delete(user);
                ErroLogging.LogError(ex);
                throw ex;
            }
        }
Exemplo n.º 7
0
        public async Task UpdateUserAsync(UpdatedDemographicModel model)
        {
            try
            {
                using (WW_DevEntities context = new WW_DevEntities())
                {
                    ApplicationUser user = await FindAspUserByUserId(HttpContext.Current.User.Identity.Name);

                    if (user != null)
                    {
                        Client updatedUser = context.Clients.Where(q => q.AspUserId == user.Id).FirstOrDefault();

                        if (updatedUser != null)
                        {
                            updatedUser.FirstName = ProperCase.Convert(model.FirstName);
                            updatedUser.MiddleName = ProperCase.Convert(model.MiddleName);
                            updatedUser.LastName = ProperCase.Convert(model.LastName);
                            updatedUser.Suffix = ProperCase.Convert(model.Suffix);
                            //updatedUser.SSN = String.IsNullOrWhiteSpace(model.Identification.Ssn) ? null : model.Identification.Ssn;
                            //updatedUser.DateOfBirth = model.BirthInfo.DateOfBirth;
                            //updatedUser.Gender = model.Appearance.Gender;
                            updatedUser.UpdatedDate = DateTime.Now;

                            //var phoneQuery = (from pAss in context.ClientPhoneAssociatives
                            //                  join p in context.ClientPhones
                            //                  on pAss.ClientPhoneGenID equals p.ClientPhoneGenID
                            //                  join pType in context.ref_PhoneTypes
                            //                  on pAss.PhoneTypeID equals pType.PhoneTypeGenId
                            //                  where pAss.ClientGenID == updatedUser.Id
                            //                  select new
                            //                  {
                            //                      PhoneId = p.ClientPhoneGenID,
                            //                      PhoneNumber = p.PhoneNumber,
                            //                      PhoneTypeID = pAss.PhoneTypeID,
                            //                      PhoneType = pType.PhoneTypeDescription
                            //                  }).ToList();


                            //if (phoneQuery.Any())
                            //{
                            //    foreach (var p in phoneQuery)
                            //    {
                            //        if (p.PhoneTypeID == 1)
                            //        {
                            //            var updatedPhone = context.ClientPhones.Where(q => q.ClientPhoneGenID == p.PhoneId).FirstOrDefault();
                            //            updatedPhone.PhoneNumber = model.Phone.WorkPhone;
                            //        }
                            //        else if (p.PhoneTypeID == 2)
                            //        {
                            //            var updatedPhone = context.ClientPhones.Where(q => q.ClientPhoneGenID == p.PhoneId).FirstOrDefault();
                            //            updatedPhone.PhoneNumber = model.Phone.HomePhone;
                            //        }
                            //        else if (p.PhoneTypeID == 3)
                            //        {
                            //            var updatedPhone = context.ClientPhones.Where(q => q.ClientPhoneGenID == p.PhoneId).FirstOrDefault();
                            //            updatedPhone.PhoneNumber = model.Phone.MobilePhone;
                            //        }
                            //    }
                            //}

                            //if (model.Phone.MobilePhone != null)
                            //{
                            //    user.PhoneNumber = model.Phone.MobilePhone;
                            //    UserManager.Update(user);
                            //}

                            if (await context.SaveChangesAsync() > 0)
                                await SendNotificationEmail(user.Id, EmailMessage.NotificationUpdateDemographic);
                        }
                    }
                }
            }
            catch (DbEntityValidationException ex)
            {
                // Retrieve the error messages as a list of strings.
                var errorMessages = ex.EntityValidationErrors
                        .SelectMany(x => x.ValidationErrors)
                        .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                ErroLogging.LogError(ex);
                // Throw a new DbEntityValidationException with the improved exception message.
                throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
            }
            catch (Exception ex)
            {
                ErroLogging.LogError(ex);
                throw ex;
            }
        }
Exemplo n.º 8
0
 public Client FindClientInfoByAspUserId(string AspUserId)
 {
     using (WW_DevEntities context = new WW_DevEntities())
     {
         return context.Clients.Where(a => a.AspUserId == AspUserId).FirstOrDefault();
     }
 }
Exemplo n.º 9
0
 public async Task SendChangePasswordConfirmation(string userId, string email)
 {
     try
     {
         using (WW_DevEntities context = new WW_DevEntities())
         {
             Client user = context.Clients.Where(q => q.AspUserId == userId).FirstOrDefault();
             string code = HttpContext.Current.Server.UrlEncode(await UserManager.GeneratePasswordResetTokenAsync(userId));
             var callbackUrl = "<a href='" + BaseUrl.Base + "api/Account/CheckPasswordCode?userId=" + userId + "&code=" + code + "&email=" + email + "'>Confirm Password Change</a>";
             var emailSubject = EmailMessage.ChangePasswordSubject;
             var emailContent = EmailMessage.ConfirmPasswordChangeBody(user.FirstName, callbackUrl);
             await UserManager.SendEmailAsync(userId, emailSubject, emailContent);
         }
     }
     catch (Exception ex)
     {
         ErroLogging.LogError(ex);
         throw ex;
     }
 }
Exemplo n.º 10
0
        public async Task<userInfo> CheckTempGUID(string id)
        {
            userInfo user = null;
            Client client = null;

            try
            {
                using (WW_DevEntities db = new WW_DevEntities())
                {
                    client = db.Clients.Where(q => q.TempGUID == id).FirstOrDefault();

                    //couldn't find user in client table
                    if (client == null)
                        return null;

                    ApplicationUser aspUser = await FindAspUserByUserId(client.AspUserId);

                    //couldn't find user in asp user table
                    if (aspUser == null)
                        return null;

                    if (client.TempGUIDExpire > DateTime.Now)
                    {
                        user = new userInfo
                        {
                            FirstName = client.FirstName,
                            LastName = client.LastName,
                            UserName = aspUser.UserName,
                            Roles = new List<string>(),
                            AspUserId = client.AspUserId,
                            CreateDate = client.CreatedDate.ToShortDateString()
                        };

                        user.Roles.AddRange(await FindRolesByAspUserId(client.AspUserId));
                    }

                    client.TempGUID = null;
                    client.TempGUIDExpire = null;

                    db.SaveChanges();
                }

                return user;
            }
            catch (Exception ex)
            {
                ErroLogging.LogError(ex);
                throw ex;
            }
        }
Exemplo n.º 11
0
        public string SetTempGUID(ApplicationUser model)
        {
            try
            {
                Client client;
                double minutes = double.Parse(Config.TempGUIDLengthInMinutes);
                DateTime guidExpirationTime = DateTime.Now.AddMinutes(minutes);

                using (WW_DevEntities db = new WW_DevEntities())
                {
                    client = db.Clients.Where(q => q.AspUserId == model.Id).FirstOrDefault();

                    if (client == null)
                        return "There was no client with that Id.";

                    client.TempGUID = Guid.NewGuid().ToString();
                    client.TempGUIDExpire = guidExpirationTime;

                    db.SaveChanges();

                    return client.TempGUID;
                }
            }
            catch (Exception ex)
            {
                ErroLogging.LogError(ex);
                throw ex;
            }
        }
Exemplo n.º 12
0
        public async Task SendTwoFactorAuthentionToken(string userId)
        {
            try
            {
                using (WW_DevEntities context = new WW_DevEntities())
                {
                    Client user = context.Clients.Where(q => q.AspUserId == userId).FirstOrDefault();
                    string pin = await GetTwoFactorCode(userId);
                    var emailSubject = EmailMessage.TFASubject;
                    string emailBody = EmailMessage.TFAEmailBody(user.FirstName, pin);

                    await UserManager.SendEmailAsync(userId, emailSubject, emailBody);
                }
            }
            catch (Exception ex)
            {
                ErroLogging.LogError(ex);
                throw ex;
            }
        }
Exemplo n.º 13
0
        public async Task SendConfirmationEmail(string AspUserId, string confirmationType)
        {
            try
            {
                using (WW_DevEntities context = new WW_DevEntities())
                {
                    Client user = context.Clients.Where(q => q.AspUserId == AspUserId).FirstOrDefault();
                    string code = HttpContext.Current.Server.UrlEncode(UserManager.GenerateEmailConfirmationToken(AspUserId));
                    var callbackUrl = "<a href='" + BaseUrl.Base + "api/Account/ConfirmEmail?aspUserId=" + AspUserId + "&code=" + code + "' >Activation Link</a>";
                    var emailSubject = EmailMessage.ConfirmationSubject;

                    var emailContent = EmailMessage.ConfirmationEmailBody(user.FirstName, callbackUrl, confirmationType);

                    await UserManager.SendEmailAsync(AspUserId, emailSubject, emailContent);
                }
            }
            catch (Exception ex)
            {
                ErroLogging.LogError(ex);
                throw ex;
            }
        }
Exemplo n.º 14
0
 public async Task MarkLogInDateTime(string AspUserId)
 {
     using (WW_DevEntities context = new WW_DevEntities())
     {
         Client currentUser = context.Clients.Where(a => a.AspUserId == AspUserId).FirstOrDefault();
         currentUser.LastLogInDate = DateTime.Now;
         await context.SaveChangesAsync();
     }
 }
Exemplo n.º 15
0
        public async Task SendNotificationEmail(string AspUserId, string notificationType)
        {
            try
            {
                using (WW_DevEntities context = new WW_DevEntities())
                {
                    Client user = context.Clients.Where(q => q.AspUserId == AspUserId).FirstOrDefault();
                    var emailSubject = EmailMessage.ChangeNotificationSubject;
                    var emailContent = EmailMessage.ChangeNotificationBody(user.FirstName, notificationType);

                    await UserManager.SendEmailAsync(AspUserId, emailSubject, emailContent);
                }
            }
            catch (Exception ex)
            {
                ErroLogging.LogError(ex);
                throw ex;
            }
        }
Exemplo n.º 16
0
        public async Task SendEmailChangeNotificationEmail(string AspUserId, string oldEmail)
        {
            try
            {
                using (WW_DevEntities context = new WW_DevEntities())
                {
                    Client user = context.Clients.Where(q => q.AspUserId == AspUserId).FirstOrDefault();

                    if (user != null)
                    {
                        EmailService es = new EmailService();
                        IdentityMessage im = new IdentityMessage();
                        im.Destination = oldEmail;
                        im.Body = EmailMessage.ChangeNotificationBody(user.FirstName, EmailMessage.NotificationChangeEmailOld);
                        im.Subject = EmailMessage.ChangeNotificationSubject;
                        await es.SendAsync(im);

                        var emailSubject = EmailMessage.ChangeNotificationSubject;
                        var emailContent = EmailMessage.ChangeNotificationBody(user.FirstName, EmailMessage.NotificationChangeEmailNew);

                        await UserManager.SendEmailAsync(AspUserId, emailSubject, emailContent);
                    }
                }
            }
            catch (Exception ex)
            {
                ErroLogging.LogError(ex);
                throw ex;
            }
        }
Exemplo n.º 17
0
        public async Task<UpdatedDemographicModel> DemographicUserGetAsync(String AspUserId)
        {
            try
            {
                ApplicationUser aspUser = await FindAspUserByUserId(AspUserId);

                UpdatedDemographicModel user = new UpdatedDemographicModel();
                Client client = new Client();

                using (WW_DevEntities context = new WW_DevEntities())
                {
                    client = context.Clients.Where(q => q.AspUserId == AspUserId).FirstOrDefault();

                    if (client != null)
                    {
                        // TODO:
                        // implement dob
                        user.FirstName = client.FirstName;
                        user.MiddleName = client.MiddleName;
                        user.LastName = client.LastName;
                        user.Suffix = client.Suffix;
                        //user.SSN = client.SSN;
                        //user.DateOfBirth = client.DateOfBirth.Value.ToShortDateString() ?? client.DateOfBirth.Value.ToShortDateString();
                        user.Email = aspUser.Email;
                    }
                }

                return user;
            }
            catch (Exception ex)
            {
                ErroLogging.LogError(ex);
                throw ex;
            }
        }