Example #1
0
        public UserAuthenticationModel Authenticate(string userName, string passWord)
        {
            var model = new UserAuthenticationModel();
            var encryptedPassword = HashPassword(passWord);
            using (var ctx = new FightFleetDataContext())
            {
                var user = ctx.Users.FirstOrDefault(c => c.UserName.ToLower() == userName.ToLower()
                    && c.Password == encryptedPassword);
                if (user == null)
                    return model;

                model.UserName = user.UserName;
                model.UserId = user.UserId;
                var authentication = user.Authentications.FirstOrDefault(c => c.ExpiresOn >= DateTime.Now);
                if (authentication == null )
                {
                    authentication = new Authentication
                    {
                        ExpiresOn = DateTime.Now.AddDays(7),
                        AccessToken = Guid.NewGuid(),
                        CreatedOn = DateTime.Now,

                    };
                    user.Authentications.Add(authentication);
                    ctx.SubmitChanges();
                }
                else if ((DateTime.Now - authentication.ExpiresOn).TotalDays <= 1)
                {
                    authentication.ExpiresOn = DateTime.Now.AddDays(7);
                    ctx.SubmitChanges();
                }
                model.AccessToken = authentication.AccessToken;
            }

            return model;
        }
 partial void DeleteAuthentication(Authentication instance);
 partial void InsertAuthentication(Authentication instance);
 partial void UpdateAuthentication(Authentication instance);
		private void detach_Authentications(Authentication entity)
		{
			this.SendPropertyChanging();
			entity.User = null;
		}