繁体   English   中英

通过RESTful和JSON格式的Yii用户身份验证

[英]Yii User Authentication via RESTful with JSON format

我希望能够针对电子邮件和数据库中数据库中的用户表对Yii中的用户进行身份验证。 我可以在Yii中执行此操作,但是现在我希望能够通过RESTful和JSON格式对同一用户进行身份验证。 以下是我的用于身份验证的Yii代码,位于Yii defaut LoginForm模型中:

/**
 * 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;

}

我怎样才能做到这一点?

该身份验证基于Cookie / SESSION机制,因此不适用于API查询。 我建议您将查询身份验证与其他签名参数一起使用。 请参阅此参考文章 简而言之,您应该接收用户登录名和密码哈希,使用db数据对其进行检查,然后向客户端返回一些随机生成的令牌,该令牌将用于对所有其他查询进行签名(将此令牌保存到db并进行检查)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM