yii2登录重定向后丢失用户身份

编程入门 行业动态 更新时间:2024-10-26 04:30:20
本文介绍了yii2登录重定向后丢失用户身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我查看了其他类似的问题,而这个问题( Yii2页面重定向后用户身份丢失)提出了几乎相同的问题,没有适用于我的情况的解决方案.

i have looked at the other similar questions, and while this one (Yii2 user identity loss after page redirection) asks pretty much the same question, there is no solution that applies to my situation.

我创建了一个新的标识类,我已经实现了所有必需的方法并实现了IdentityInterface.请注意,它不会扩展ActiveRecord,而是扩展了我自己的,不是从AR派生的基本Model类.

i have created a new identity class, i have implemented all the necessary methods and implemented the IdentityInterface. note that it does NOT extend ActiveRecord, but rather extends my own base Model class that does not derive from AR.

一切似乎都正常,我可以登录并进行正确的身份验证.我已经跟踪了代码,可以看到在调用IdentityInterface :: validatePassword()和User :: login()之后,在Yii :: $ app-> user中正确设置了我的身份类.

everything seems to work fine, i can log in and be correctly authenticated. i have traced the code and i can see that my identity class is correctly set in Yii::$app->user after IdentityInterface::validatePassword() and User::login() have been called.

但是,一旦登录成功并且用户被重定向,Yii :: $ app-> user包含一个空的IdentityInterface,而不是重定向之前的那个空接口.

however, once the login succeeds and the user is redirected, Yii::$app->user contains an empty IdentityInterface, rather than the one that was there immediately prior to the redirect.

在SiteController :: actionLogin()中,我修改了以下代码:

in SiteController::actionLogin(), i have modified the code from:

return $this->goHome();

收件人:

$tmp = $this->goHome(); // echo '<pre>';var_dump(Yii::$app->user);echo '</pre>';exit; return($tmp);

并且我可以看到Yii :: $ app->用户在return()语句之前仍然具有正确的身份.

and i can see that Yii::$app->user still has the correct identity up until the return() statement.

有人可以告诉我为什么我失去身份吗?我已经找到了所有可以想到的内容:yii \ web \ Controller,yii \ base \ Controller,yii \ web \ Response,SiteController,yii \ web \ User等...

can anyone tell me why i am losing my identity? i have traced through everything i can think of: yii\web\Controller, yii\base\Controller, yii\web\Response, SiteController, yii\web\User, etc...

任何帮助都将不胜感激.谢谢!

any help at all would be greatly appreciated. thanks!

推荐答案

相同的问题,三天后找到解决方案,并且它可以正常工作. 我的用户模型是:

Same problem, after 3 days find solution and I it working. My User model is:

namespace app\models; use Yii; class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface { public $username; /** * @inheritdoc */ public static function tableName() { return 'tbl_users'; } /** * @inheritdoc */ public function rules() { return [ [['name', 'email', 'password'], 'required'], [['email'], 'unique'], [['role', 'status'], 'integer'], [['created', 'last_update'], 'safe'], [['name', 'email', 'password'], 'string', 'max' => 250] ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'name' => 'Name', 'email' => 'Email', 'password' => 'Password', 'role' => 'Role', 'status' => 'Status', 'created' => 'Created', 'last_update' => 'Last Update', ]; } public function getAuthKey() { } public function getId() { return $this->id; } public function validateAuthKey($authKey) { } public static function findIdentity($id) { } public static function findIdentityByAccessToken($token, $type = null) { } public static function findByEmail($email) { return static::findOne(array('email'=>$email)); } public function validatePassword($password) { return $this->password === $password; } }

我更改:

public static function findIdentity($id) { }

收件人:

public static function findIdentity($id) { return self::findOne(array('id'=>$id)); }

对我有用.

更多推荐

yii2登录重定向后丢失用户身份

本文发布于:2023-11-01 19:53:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550251.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   身份   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!