简体   繁体   English

Yii2登录无密码认证

[英]Yii2 Login Authenticate without password

I am working on project that will use API of another application. 我正在研究将使用另一个应用程序的API的项目。 So I won't validate password on my app, will do this via API request. 因此,我不会在我的应用程序上验证密码,而是通过API请求进行验证。 I don't want to store users` passwords in db. 我不想在db中存储用户密码。 Just check password by API. 只需通过API检查密码即可。

How can I authenticate a user without login credentials? 没有登录凭证的情况下如何认证用户?

Login & Session in Yii 2 is managed by \\yii\\web\\User class This class is accessible in the application life cycle via the \\Yii::$app->user property. Yii 2中的登录和会话由\\yii\\web\\User类管理。在应用程序生命周期中,可以通过\\Yii::$app->user属性访问该类。

On successful authentication using your third-party provider you use the login() function using Yii::$app->user->login($identity) 使用第三方提供商成功进行身份验证后,可以使用Yii::$app->user->login($identity)使用login()函数

$identity should be an object of [IdentityInterface][4] usually the User ActiveRecord model implementing this interface $identity应该是[IdentityInterface][4]的对象,通常是实现此接口的User ActiveRecord模型

Refer to the guide documentation on Authentication for full understanding on how authentication works in Yii2 请参阅有关身份验证的指南文档 ,以全面了解Yii2中身份验证的工作方式

In our app we are using a field auth_key related to a User Model and then we go to this link, find a user in database by this auth_key and make a login (or not if it is not found in database ) 在我们的应用程序中,我们使用与用户模型相关的字段auth_key ,然后转到此链接,通过此auth_key在数据库中找到一个用户并进行登录(如果在database中找不到该用户, 则不进行登录)

public actionAuthLogin($auth_key){
  if(($user = User::findOne(['auth_key' => $auth_key])) 
        && \Yii:$app->user->login($user)){
     return $this->goHome();
  } else {

     throw new NotFoundHttpException()
  }

}

then 然后

/controller/auth-login?auth_key=should_be_in_database_ahsgdashdajsdg123mkasd

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

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