好吧,我正在申请中实施CAS登录。
问题是,在CasLogin()
函数..登录成功,所有会话变量都设置..
和Yii::app()->user->isGuest
,返回false。即正确登录。
和Yii::app()->user->id
,返回登录ID
。
然而......当我从这里重定向到我的索引操作Yii::app()->user->isGuest
时,返回true。和Yii::app()->user->id
,返回NULL。
实际上我进行了一个实验$ _session包含CasLogin()中的所有值,但是在重定向之后,它有NULL 所以会话被破坏...在重定向期间
casUserIdentity
扩展CUserIdentity
,其构造函数我在CasLoginForm()
的登录功能中调用;
顺便说一句,LocalLoginIdentity
工作得很好......这些值不会在那里复位..
虽然我发送,用户名和密码到LocalUserIdentity
构造函数..
以下是有序的代码.. 公共功能actionIndex() { echo“hello at index”;
if (! Yii::app()->user->isGuest) {
//#CASE 1: User is already logged in
$this->redirect(array("site/upload"));
}
else{
//#CASE 2: User is not Logged in
echo '<br>Always show up, and id NULL <br>';
var_dump(Yii::app()->user->id);
exit;
$this->redirect(array("site/login"));
}
}
这是代码CasLoginFunction()
:
public function actionCasLogin($CID=NULL)
{
//code to be imported from GMS here
PhpCasControl::setPhpCasContext($CID);
phpCAS::setPostAuthenticateCallback(array($this,'_saveServiceTkt'));
$loginForm = new CasLoginForm;
// validate user input and redirect to the previous page if valid
if ($loginForm->login($CID)) {
if (Yii::app()->user->isGuest){
echo 'this always show up, if at this controller only';
var_dump(Yii::app()->user->id);}
else{
echo 'it never showed up';
var_dump(Yii::app()->user->id);}
$this->redirect(array('index'));
}
else {
throw new Exception("You don't have sufficient permissions to access this service. Please contact Administrator !");
}
}
以下是我调用CasUserIdentity()的代码 class CasLoginForm扩展了CFormModel {
private $_identity; //User identity
/**
* Logs in the user using the cas forceAuthentication method
* @return boolean whether login is successful
*/
public function login($cname=NULL) {
if ($this->_identity === null) {
$this->_identity = new CasUserIdentity('', NULL);
$this->_identity->authenticate($cname);
var_dump($this->_identity);
}
if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
$duration = 0; // 0 days
Yii::app()->user->login($this->_identity, $duration);
//get service tkt from session
$serviceTkt = Yii::app()->user->getState('service_tkt');
$this->rename_session($serviceTkt);
//echo "<br> current_session <br><br>".$serviceTkt."<br>";
//var_dump($_SESSION);
return true;
}
else
//echo "hies";
return false;
}
此函数返回True ..所以不必担心CAS
答案 0 :(得分:1)
为什么你没有使用CUserIdentity类? ,但在您当前的身份验证代码中,只需添加以下代码即可解决此问题。
Yii::app()->user->setState('id',$userId);// assign user id
我建议你阅读这篇对你有帮助的文章