public void SignIn(string userName, bool createPersistentCookie, User user)
 {
     if (String.IsNullOrEmpty(userName) || user == null)
     throw new ArgumentException("Value cannot be null or empty.", "userName");
       SignIn(userName, createPersistentCookie, user.ID);
       AppHelper.CurrentUser = SessionUser.Create(user);
 }
    public void SignIn(string userName, bool createPersistentCookie, User user)
    {
      if (String.IsNullOrEmpty(userName) || user == null)
        throw new ArgumentException("Value cannot be null or empty.", "userName");

      FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddMinutes(Consts.FormsAuthenticationTicketTime), createPersistentCookie, String.Format("{0}|{1}|{2}", user.ID, DateTime.Now, createPersistentCookie), FormsAuthentication.FormsCookiePath);
      string encTicket = FormsAuthentication.Encrypt(authTicket);
      HttpContext.Current.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket) { HttpOnly = true });

      AppHelper.CurrentUser = SessionUser.Create(user);

      // begin 2013-03-17 -> check added
      Logger.LogInfo(String.Format("[LOG-IN] Id: {0} | Email: {1} | IP: {2} | Local IP: {3} | SessionID: {4}\n\t\t\tUser Agent: {5}", user.ID, user.Email, user.IP, HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"], HttpContext.Current.Session.SessionID, HttpContext.Current.Request.UserAgent));
      // end 2013-03-17 -> check added
    }
 //SubscribeRegisterUser
 public bool SubscribeRegisterUser(User u)
 {
   try
   {
     if (u == null) throw new Exception("The user doesn't exist");
     OuterSubscription os = dataContext.OuterSubscriptions.SingleOrDefault(OU => OU.Email == u.Email);
     if (os != null)
     {
       os.IsActive = true;
       os.IsRecievingUpdates = u.RecieveNewsUpdates.GetValueOrDefault(false);
       os.IsRecievingWeeklySpecials = u.RecieveWeeklySpecials.GetValueOrDefault(false);
       SubmitChanges();
       return false;
     }
     os = new OuterSubscription();        
     os.Email = u.Email;
     os.EmailConfirm = u.Email;
     os.IsActive = true;        
     os.IsRecievingUpdates = u.RecieveNewsUpdates.GetValueOrDefault(false);
     os.IsRecievingWeeklySpecials = u.RecieveWeeklySpecials.GetValueOrDefault(false);
     os.FirstName = u.AddressCard_Billing.FirstName;
     os.LastName = u.AddressCard_Billing.LastName;
     dataContext.OuterSubscriptions.InsertOnSubmit(os);
     SubmitChanges();
   }
   catch (Exception ex)
   {
     Logger.LogException("[user_id=" + u != null ? u.ID.ToString() : "null" + "]", ex);
     return false;
   }
   return true;
 }
    //AddUser
    public User AddUser(RegisterInfo info)
    {
      User user = new User();
      try
      {
        dataContext.Users.InsertOnSubmit(user);
        user.Login = info.Login;
        user.Email = info.Email;
        user.ConfirmationCode = Guid.NewGuid().ToString().Replace("-", "");
        user.Password = info.Password;
        user.DateRegistered = DateTime.Now;
        user.UserType = (byte)Consts.UserTypes.Buyer;
        user.IsConfirmed = false;        
        user.Fax = info.Fax;        
        user.BillingLikeShipping = info.BillingLikeShipping;
        user.Status = (byte)Consts.UserStatus.Pending;
        user.MobilePhone = info.MobilePhone;
        user.TaxpayerID = info.TaxpayerID;
        user.EbayID = info.EbayID;
        user.EbayFeedback = info.EbayFeedback;        
        user.DayPhone = info.DayPhone;
        user.EveningPhone = info.EveningPhone;
        user.IsModifyed = true;
        user.IsRecievingOutBidNotice = info.RecievingOutBidNotice;
        user.CommissionRate_ID = Consts.DefaultCommissionRate;
        user.RecieveNewsUpdates = true;
        user.RecieveWeeklySpecials = true;
        user.IsRecievingBidConfirmation = true;
        user.IsRecievingLotClosedNotice = true;
        user.IsRecievingLotSoldNotice = true;

        IDifferentRepository diff = new DifferentRepository(dataContext, CacheRepository);
        State BillingState = diff.GetStateByCode(info.BillingState);
        State ShippingState;
        long state1 = (BillingState == null) ? 0 : BillingState.ID; ;
        long state2;
        if (!info.BillingLikeShipping)
        {
          ShippingState = diff.GetStateByCode(info.ShippingState);
          state2 = (ShippingState == null) ? 0 : ShippingState.ID;
        }
        else state2 = state1;

        AddressCard ac = new AddressCard();
        dataContext.AddressCards.InsertOnSubmit(ac);
        ac.FirstName = info.BillingFirstName;
        ac.LastName = info.BillingLastName;
        ac.MiddleName = info.BillingMIName;
        ac.Address1 = info.BillingAddress1;
        ac.Address2 = info.BillingAddress2;
        ac.State = String.IsNullOrEmpty(info.BillingState) ? String.Empty : info.BillingState;
        ac.City = info.BillingCity;
        ac.Zip = info.BillingZip;
        ac.Country_ID = info.BillingCountry;
        ac.Company = info.BillingCompany;
        ac.InternationalState = info.BillingInternationalState;
        ac.HomePhone = ac.WorkPhone = info.BillingPhone;
        ac.State_ID = state1;
        user.AddressCard_Billing = ac;

        ac = new AddressCard();
        dataContext.AddressCards.InsertOnSubmit(ac);
        ac.FirstName = info.BillingFirstName;
        ac.LastName = info.BillingLastName;
        ac.MiddleName = info.BillingMIName;
        ac.Address1 = (user.BillingLikeShipping) ? info.BillingAddress1 : info.ShippingAddress1;
        ac.Address2 = (user.BillingLikeShipping) ? info.BillingAddress2 : info.ShippingAddress2;
        ac.State = (user.BillingLikeShipping) ? info.BillingState : info.ShippingState;
        ac.City = (user.BillingLikeShipping) ? info.BillingCity : info.ShippingCity;
        ac.Zip = (user.BillingLikeShipping) ? info.BillingZip : info.ShippingZip;
        ac.Country_ID = (user.BillingLikeShipping) ? info.BillingCountry : info.ShippingCountry;
        ac.InternationalState = (user.BillingLikeShipping) ? info.BillingInternationalState : info.ShippingInternationalState;
        ac.State_ID = (user.BillingLikeShipping) ? state1 : state2;
        ac.HomePhone = ac.WorkPhone = (user.BillingLikeShipping) ? info.BillingPhone : info.ShippingPhone;
        ac.State_ID = (user.BillingLikeShipping) ? state1 : state2;
        user.AddressCard_Shipping = ac;

        UserReference ur = new UserReference();
        dataContext.UserReferences.InsertOnSubmit(ur);
        ur.AuctionHouse = info.Reference1AuctionHouse;
        ur.PhoneNumber = info.Reference1PhoneNumber;
        ur.LastBidPlaced = info.Reference1LastBidPlaced;
        user.UserReference = ur;

        ur = new UserReference();
        dataContext.UserReferences.InsertOnSubmit(ur);
        ur.AuctionHouse = info.Reference2AuctionHouse;
        ur.PhoneNumber = info.Reference2PhoneNumber;
        ur.LastBidPlaced = info.Reference2LastBidPlaced;
        user.UserReference1 = ur;

        SubmitChanges();
        RemoveUserFromCache(user.ID, user.Email);
      }
      catch (Exception ex)
      {
        Logger.LogException(ex);
      }
      return user;
    }
    //UpdateUser
    public User UpdateUser(RegisterInfo info)
    {
      User usr = GetUser(info.ID, false);
      try
      {
        if (usr == null)
        {
          usr = new User();
          dataContext.Users.InsertOnSubmit(usr);
        }

        usr.Login = info.Login;
        usr.Email = info.Email;
        usr.Password = info.Password;
        usr.RecieveNewsUpdates = info.RecieveNewsUpdates;
        usr.RecieveWeeklySpecials = info.RecieveWeeklySpecials;
        usr.Fax = info.Fax;        
        usr.BillingLikeShipping = info.BillingLikeShipping;
        usr.MobilePhone = info.MobilePhone;
        usr.TaxpayerID = info.TaxpayerID;
        usr.EbayID = info.EbayID;
        usr.EbayFeedback = info.EbayFeedback;
        usr.DayPhone = info.DayPhone;
        usr.EveningPhone = info.EveningPhone;        
        usr.IsModifyed = true;        
        
        State BillingState, ShippingState;
        long state1, state2;

        IDifferentRepository diff = new DifferentRepository(dataContext, CacheRepository);
        BillingState = (!String.IsNullOrEmpty(info.BillingState)) ? diff.GetStateByCode(info.BillingState.ToLower()) : null;
        state1 = (BillingState == null) ? 0 : BillingState.ID;        
        if (!info.BillingLikeShipping)
        {
          ShippingState = (!String.IsNullOrEmpty(info.ShippingState)) ? diff.GetStateByCode(info.ShippingState.ToLower()) : null;
          state2 = (ShippingState == null) ? 0 : ShippingState.ID;
        }
        else state2 = state1;

        IAddressCard ac1 = GetAddressCard(usr.Billing_AddressCard_ID.GetValueOrDefault(-1), false);        
        if (ac1 == null)
        {
          ac1 = new AddressCard();
          dataContext.AddressCards.InsertOnSubmit(ac1 as AddressCard);
          usr.AddressCard_Billing = ac1 as AddressCard;
        }
        ac1.FirstName = info.BillingFirstName;
        ac1.LastName = info.BillingLastName;
        ac1.MiddleName = info.BillingMIName;
        ac1.Address1 = info.BillingAddress1;
        ac1.Address2 = info.BillingAddress2;
        ac1.City = info.BillingCity;
        ac1.State = String.IsNullOrEmpty(info.BillingState) ? String.Empty : info.BillingState;
        ac1.Zip = info.BillingZip;
        ac1.Country_ID = info.BillingCountry;
        ac1.Company = info.BillingCompany;
        ac1.InternationalState = info.BillingInternationalState;
        ac1.HomePhone = ac1.WorkPhone = info.BillingPhone;
        ac1.State_ID = state1;

        IAddressCard ac = usr.Shipping_AddressCard_ID.HasValue ? GetAddressCard(usr.Shipping_AddressCard_ID.GetValueOrDefault(-1), false) : GetAddressCard(usr.Billing_AddressCard_ID.GetValueOrDefault(-1), false);
        if (ac == null)
        {
          ac = new AddressCard();
          dataContext.AddressCards.InsertOnSubmit(ac as AddressCard);
          usr.AddressCard_Shipping = ac as AddressCard;
        }
        ac.FirstName = info.BillingFirstName;
        ac.LastName = info.BillingLastName;
        ac.MiddleName = info.BillingMIName;
        ac.Address1 = (usr.BillingLikeShipping) ? info.BillingAddress1 : info.ShippingAddress1;
        ac.Address2 = (usr.BillingLikeShipping) ? info.BillingAddress2 : info.ShippingAddress2;
        ac.City = (usr.BillingLikeShipping) ? info.BillingCity : info.ShippingCity;
        ac.State = (usr.BillingLikeShipping) ? info.BillingState : info.ShippingState;
        ac.State = String.IsNullOrEmpty(ac.State)?String.Empty:ac.State;
        ac.InternationalState = (usr.BillingLikeShipping) ? info.BillingInternationalState : info.ShippingInternationalState;
        ac.Zip = (usr.BillingLikeShipping) ? info.BillingZip : info.ShippingZip;
        ac.Country_ID = (usr.BillingLikeShipping) ? info.BillingCountry : info.ShippingCountry;
        ac.HomePhone = ac.WorkPhone = (usr.BillingLikeShipping) ? info.BillingPhone : info.ShippingPhone;
        ac.State_ID = (usr.BillingLikeShipping) ? state1 : state2;

        SubmitChanges();

        RemoveUserFromCache(usr.ID, usr.Email);
      }
      catch (Exception ex)
      {
        Logger.LogException(ex);
        throw ex;
      }
      return usr;
    }
 //UpdateUserObjectCache
 private void UpdateUserObjectCache(User user)
 {
   CacheRepository.Put(new DataCacheObject(DataCacheType.ACTIVITY, DataCacheRegions.USERS, "GETUSER", new object[] { user.ID }, CachingExpirationTime.Minutes_10, user));
 }
 //TryToUpdateNormalAttempts
 public void TryToUpdateNormalAttempts(User usr)
 {
   try
   {
     if (usr == null) throw new Exception("The user doesn't exist");
     usr.FailedAttempts = 0;
     usr.IP = Consts.UsersIPAddress;
     usr.LastAttempt = DateTime.Now;
     SubmitChanges();
   }
   catch (Exception ex)
   {
     Logger.LogException("[user_id=" + usr != null ? usr.ID.ToString() : "null" + "]", ex);
   }
 }
 //Create
 public static SessionUser Create(User user)
 {
     return new SessionUser(user.ID, user.Login, user.AddressCard_Billing.FirstName, user.UserType_ID, user.UserStatus_ID, user.IsRecievingBidConfirmation, user.IsRecievingOutBidNotice, user.IsRecievingLotSoldNotice, user.IsRecievingLotClosedNotice, user.RecieveWeeklySpecials.GetValueOrDefault(false), user.RecieveNewsUpdates.GetValueOrDefault(false), user.IP);
 }