Пример #1
0
        public static void Add_UpdateToken(int userId, TokenDetails _token, int forceupdate = 0, string deviceType = null)
        {
            try
            {
                LystenEntities db       = new LystenEntities();
                var            chkToken = db.AppAccessTokens.Where(x => x.UserId == userId).FirstOrDefault();
                if (chkToken != null)
                {
                    if (chkToken.ExpiresOn <= DateTime.Now)
                    {
                        db.Entry(chkToken).State = EntityState.Modified;

                        TimeSpan t = TimeSpan.FromMinutes(Convert.ToInt16(ConfigurationManager.AppSettings["TokenExpireHour"].ToString()));
                        chkToken.AuthToken = _token.access_token;
                        chkToken.ExpiresOn = DateTime.Now.Add(t);
                        chkToken.IssuedOn  = DateTime.Now;

                        db.SaveChanges();
                    }
                    else
                    {
                        if (forceupdate > 0)
                        {
                            db.Entry(chkToken).State = EntityState.Modified;

                            TimeSpan t = TimeSpan.FromMinutes(Convert.ToInt16(ConfigurationManager.AppSettings["TokenExpireHour"].ToString()));
                            chkToken.AuthToken  = _token.access_token;
                            chkToken.ExpiresOn  = DateTime.Now.Add(t);
                            chkToken.IssuedOn   = DateTime.Now;
                            chkToken.DeviceType = deviceType;
                            db.SaveChanges();
                        }
                    }
                }
                else
                {
                    AppAccessToken _tokenDetails = new AppAccessToken();
                    _tokenDetails.UserId   = userId;
                    _tokenDetails.IssuedOn = DateTime.Now;
                    TimeSpan t = TimeSpan.FromMinutes(Convert.ToInt16(ConfigurationManager.AppSettings["TokenExpireHour"].ToString()));
                    _tokenDetails.ExpiresOn  = DateTime.Now.Add(t);
                    _tokenDetails.AuthToken  = _token.access_token;
                    _tokenDetails.DeviceType = deviceType;
                    db.AppAccessTokens.Add(_tokenDetails);
                    db.SaveChanges();
                }
                db.Dispose();
            }
            catch (Exception ex)
            {
            }
        }