2014-04-20 83 views
0

我希望能夠通過電子郵件和密碼對數據庫中的用戶表進行身份驗證。我可以在Yii中做到這一點,但現在我希望能夠通過JSON格式的RESTful驗證相同的用戶。下面是我的驗證的Yii代碼,這是在Yii的defaut LoginForm的模型:Yii通過JSON格式的RESTful進行用戶身份驗證

/** 
* Authenticates the password. 
* This is the 'authenticate' validator as declared in rules(). 
*/ 
public function authenticate($attribute,$params) 
{ 

    if(!$this->hasErrors()) 
    { 
     $this->_identity=new UserIdentity($this->email,$this->password); 
     if(!$this->_identity->authenticate()) 
      $this->addError('password','Incorrect email or password.'); 
    } 

} 

/** 
* Logs in the user using the given email and password in the model. 
* @return boolean whether login is successful 
*/ 
public function login() 
{ 

    if($this->_identity===null) 
    { 
     $this->_identity=new UserIdentity($this->email,$this->password); 
     $this->_identity->authenticate(); 
    } 
    if($this->_identity->errorCode===UserIdentity::ERROR_NONE) 
    { 
     $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days 
     Yii::app()->user->login($this->_identity,$duration); 
     return true; 
    } 
    else 
     return false; 

} 

我怎樣才能做到這一點?

+0

您是否找到解決方案 - 我也處於與您相同的位置 – Zabs

回答

0

該驗證基於Cookie/SESSION機制,因此它不適用於API查詢。我建議你使用帶有額外簽名參數的查詢認證。請參閱this reference article。簡而言之,您應該接收用戶登錄和密碼哈希,使用db數據檢查它們,然後返回客戶端一些隨機生成的令牌,該令牌將用於簽署所有其他查詢(將該令牌保存到數據庫並檢查它)。

相關問題