Example #1
0
        //新增令牌
        public void AddToken(Token entity)
        {
            using (var db = new HouseMarketEntities())
            {
                if (entity == null)
                {
                    return;
                }
                else
                {
                    var token = db.Tokens.SingleOrDefault(t => t.TokenID == entity.TokenID);
                    var token2 = db.Tokens.SingleOrDefault(t => t.UserID == entity.UserID);

                    if (token != null)
                    {
                        EditTokenByToken(entity);
                    }
                    else if (token2 != null)
                    {
                        DeleteToken(token2);

                        db.Tokens.Add(entity);
                        db.SaveChanges();
                    }
                    else
                    {
                        db.Tokens.Add(entity);
                        db.SaveChanges();
                    }
                }
            }
        }
Example #2
0
 //使用令牌ID编辑令牌
 public void EditTokenByToken(Token entity)
 {
     using (var db = new HouseMarketEntities())
     {
         if (entity == null)
         {
             return;
         }
         else
         {
             var token = db.Tokens.SingleOrDefault(t => t.TokenID == entity.TokenID);
             if (token == null)
             {
                 return;
             }
             else
             {
                 PropertyFunction.CopyEntity(entity, token);
                 db.SaveChanges();
             }
         }
     }
 }
Example #3
0
 //删除令牌
 public void DeleteToken(Token entity)
 {
     using (var db = new HouseMarketEntities())
     {
         if (entity == null)
         {
             return;
         }
         else
         {
             var token = db.Tokens.SingleOrDefault(t => t.TokenID == entity.TokenID);
             if (token == null)
             {
                 return;
             }
             else
             {
                 db.Tokens.Remove(token);
                 db.SaveChanges();
             }
         }
     }
 }
        //用户登录验证和发放令牌
        public static int UserLogon(string userName, string password, out string tokenID, out UserViewResult logonUser)
        {
            tokenID = string.Empty;
            logonUser = new UserViewResult();
            Database.User entity = new Database.User() { UserName = userName, Password = password };

            UserFunction userFunction = new UserFunction();
            int error = userFunction.CheckUser(entity, out logonUser);

            if (error != 0)
            {
                return error;
            }
            else
            {
                tokenID = AuthenticationFunction.CreateTokenID(logonUser);
                var newToken = new Token()
                {
                    TokenID = tokenID,
                    UserID = logonUser.UserID,
                    ExpiredTime = DateTime.Now.AddMinutes(60)
                };

                TokenFunction tokenFunction = new TokenFunction();
                tokenFunction.AddToken(newToken);

                return 0;
            }
        }
        //令牌验证
        public static View_TokenUser TokenValidation(string tokenID)
        {
            if (string.IsNullOrEmpty(tokenID))
            {
                return null;
            }
            else
            {
                TokenFunction tokenFunction = new TokenFunction();
                var tokenUser = tokenFunction.QueryTokenUser(tokenID);

                if (tokenUser == null)
                {
                    return null;
                }
                else if (tokenUser.ExpiredTime < DateTime.Now)
                {
                    tokenFunction.DeleteToken(new Token() { TokenID = tokenID });
                    return null;
                }
                else
                {
                    tokenUser.ExpiredTime = DateTime.Now.AddMinutes(60);

                    UserViewResult logonUser = new UserViewResult();
                    PropertyFunction.CopyEntity(tokenUser, logonUser);

                    MyIdentity myIdentity = new MyIdentity(logonUser);
                    MyPrincipal myPrincipal = new MyPrincipal(myIdentity);
                    SetPrincipal(myPrincipal);

                    Token newToken = new Token();
                    PropertyFunction.CopyEntity(tokenUser, newToken);
                    tokenFunction.EditTokenByToken(newToken);

                    return tokenUser;
                }
            }
        }