如何使用Laravel从Microsoft Azure AD验证数据并将其重定向到Home? [英] How to Authenticate the data from Microsoft Azure AD using Laravel and redirect it to Home?

查看:59
本文介绍了如何使用Laravel从Microsoft Azure AD验证数据并将其重定向到Home?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过laravel Web应用对Microsoft Azure AD进行身份验证.目前,我指的是使用Laravel的Azure Active Directory SSO .我设法从microsoft azure广告中检索了数据,但问题是它没有重定向到/home视图,而是重定向到登录视图.

I'm trying to authenticate Microsoft Azure AD with my laravel web app. Currently I'm referring Azure Active Directory SSO with Laravel. I managed to retrieve the data from the microsoft azure ad but the problem is it doesn't redirect to /home view instead it redirect to login view.

我有一个想法,就是将来自Microsoft的电子邮件与来自模型的电子邮件进行链接,以便它可以直接转到主页.但是我不知道如何将Microsoft数据(从提供程序)传递到控制器.到目前为止,下面的代码(在Provider中)是我所做的.

I have one idea which is to link the email from Microsoft and email from the model so that it can directly go to home page. But i dont know how to pass the Microsoft data (from provider) to controller. The code(in Provider) below is what I did so far.

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Aacotroneo\Saml2\Events\Saml2LoginEvent;
use App\User;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class SAML2ServiceProvider extends ServiceProvider
{
    
protected $namespace = 'App\Http\Controllers';
    public const HOME = '/home';
    public function register()
    {
        //
    }

    public function boot()
    {
        Event::listen('Aacotroneo\Saml2\Events\Saml2LoginEvent', function (Saml2LoginEvent $event) {

            // dd($event);
            $messageId = $event->getSaml2Auth()->getLastMessageId();
            // Add your own code preventing reuse of a $messageId to stop replay attacks

            $user = $event->getSaml2User();
            $userData = [
                'id' => $user->getUserId(),
                'attributes' => $user->getAttributes(),
                'assertion' => $user->getRawSamlAssertion()
            ];

            //dd($userData);
            $inputs = [
                'sso_user_id'  => $user->getUserId(),
                'username'     => $user->getAttribute('http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name'),
                'email'        => $user->getAttribute('http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress'),
                'first_name'   => $user->getAttribute('http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname'),
                'last_name'    => $user->getAttribute('http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname'),
                'password'     => Hash::make('anything'),
             ];

            //  dd($inputs);

            // $user = User::where('sso_user_id', $inputs['sso_user_id'])->where('email', $inputs['email'])->first();
            // if(!$user){
            //     $res = PortalUser::store($inputs);
            //     if($res['status'] == 'success'){
            //         $user  = $res['data'];
            //         Auth::guard('web')->login($user);
            //     }else{
            //         Log::info('SAML USER Error '.$res['messages']);
            //     }
            // }else{
                Auth::guard('web')->login($user);
            // }

        });
    }
}

任何人都可以在这个问题上帮助我.非常感谢.

Can anyone please help me on this issue. Thank you very much.

推荐答案

我的同事为此提供了帮助,下面是他将Microsoft电子邮件与模型电子邮件联系起来的解决方案

My colleague help me on this and below are the solution where he link the Microsoft email with email from the model


namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Aacotroneo\Saml2\Events\Saml2LoginEvent;
use App\User;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class SAML2ServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */

    protected $namespace = 'App\Http\Controllers';

    public const HOME = '/home';

    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        Event::listen('Aacotroneo\Saml2\Events\Saml2LoginEvent', function (Saml2LoginEvent $event) {

            // dd($event);
            // $messageId = $event->getSaml2Auth()->getLastMessageId();
            // Add your own code preventing reuse of a $messageId to stop replay attacks

            $user = $event->getSaml2User();
            // $userData = [
            //     'id' => $user->getUserId(),
            //     'attributes' => $user->getAttributes(),
            //     'assertion' => $user->getRawSamlAssertion()
            // ];

            // dd($userData);
            $inputs = [
                'sso_user_id'  => $user->getUserId(),
                'username'     => $user->getAttribute('http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name'),
                'email'        => $user->getAttribute('http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress'),
                'first_name'   => $user->getAttribute('http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname'),
                'last_name'    => $user->getAttribute('http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname'),
                'password'     => Hash::make('anything'),
             ];

            //  dd($inputs['email'][0]);



            $user = User::where('email', $inputs['email'][0])->first();
            // dd($user->id);

            if(!$user){
               return view ('404');
            }else{
                Auth::loginUsingId($user->id);
                session()->regenerate();
            }

        });
    }
}

这篇关于如何使用Laravel从Microsoft Azure AD验证数据并将其重定向到Home?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆