Search code examples
laravelazure-active-directorylaravel-socialite

AADSTS50011: The reply URL specified in the request does not match the reply in Laravel Socialite


I am using Laravel-5.8 for User Azure AD Authentication. I installed this package:

"socialiteproviders/microsoft-azure": "^3.0",

composer require socialiteproviders/microsoft-azure

I also did the configuration. The regular Login from database is working perfecting, but I have issues with the Laravel microsoft-azure integration.

Login Controller

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Socialite;

class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/dashboard';

public function __construct()
{
    $this->middleware('guest')->except('logout');
}

public function redirectToProvider()
{
    return Socialite::with('azure')->redirect();
}

public function handleProviderCallback()
{
    $user = Socialite::with('azure')->user();
}

view blade:

login.blade

<form class="login-form" method="POST" action="{{ route('login') }}">
    {{ csrf_field() }}

    <div class=" w3l-form-group{{ $errors->has('email') ? ' has-error' : '' }}">
        <label>Username:</label>
        <div class="group">
            <i class="fas fa-user"></i>
            <input id="email" type="text" class="form-control" name="email" placeholder="Email" required autofocus>
        <div>
        </div>
        </div>
    </div>
    <div>
        @if ($errors->has('email'))
            <span class="help-block" style="color: red">
                <strong>{{ $errors->first('email') }}</strong>
            </span>
        @endif
    </div>

    <div class=" w3l-form-group{{ $errors->has('password') ? ' has-error' : '' }}">
        <label>Password:</label>
        <div class="group">
            <i class="fas fa-unlock"></i>
            <input id="password" type="password" class="form-control" name="password" placeholder="Password"  required>
            @if ($errors->has('password'))
                <span class="help-block">
                    <strong>{{ $errors->first('password') }}</strong>
                </span>
            @endif
        </div>
    </div>
    <button type="submit">Login</button>
    <a href="{{ route('azure.login') }}" class="btn btn-primary"> Azure Login</a>
</form>

I am using Laravel-5.8 for User Azure AD Authentication. I installed this package:

"socialiteproviders/microsoft-azure": "^3.0",

composer require socialiteproviders/microsoft-azure

I also did the configuration. The regular Login from database is working perfecting, but I have issues with the Laravel microsoft-azure integration.

Login Controller

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Socialite;

class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/dashboard';

public function __construct()
{
    $this->middleware('guest')->except('logout');
}

public function redirectToProvider()
{
    return Socialite::with('azure')->redirect();
}

public function handleProviderCallback()
{
    $user = Socialite::with('azure')->user();
}

login.blade

<form class="login-form" method="POST" action="{{ route('login') }}">
    {{ csrf_field() }}

    <div class=" w3l-form-group{{ $errors->has('email') ? ' has-error' : '' }}">
        <label>Username:</label>
        <div class="group">
            <i class="fas fa-user"></i>
            <input id="email" type="text" class="form-control" name="email" placeholder="Email" required autofocus>
        <div>
        </div>
        </div>
    </div>
    <div>
        @if ($errors->has('email'))
            <span class="help-block" style="color: red">
                <strong>{{ $errors->first('email') }}</strong>
            </span>
        @endif
    </div>

    <div class=" w3l-form-group{{ $errors->has('password') ? ' has-error' : '' }}">
        <label>Password:</label>
        <div class="group">
            <i class="fas fa-unlock"></i>
            <input id="password" type="password" class="form-control" name="password" placeholder="Password"  required>
            @if ($errors->has('password'))
                <span class="help-block">
                    <strong>{{ $errors->first('password') }}</strong>
                </span>
            @endif
        </div>
    </div>
    <button type="submit">Login</button>
    <a href="{{ route('azure.login') }}" class="btn btn-primary"> Azure Login</a>
</form>

Expand snippet

route/web.php

Route::get('login/azure', 'Auth\LoginController@redirectToProvider')->name('azure.login');
Route::get('login/azure/callback', 'Auth\LoginController@handleProviderCallback');

config/services

'azure' => [
    'client_id' => env('AZURE_KEY','hsdhsdhsd'),
    'client_secret' => env('AZURE_SECRET','jfhrbdjf'),
    'redirect' => env('AZURE_REDIRECT_URI','https://laravelproject.laraapps.net/login/azure/callback')
],

It in production, so I am no more using .env

config/app

\SocialiteProviders\Manager\ServiceProvider::class,

When I clicked on Azure Login button, I got this error in the screenshot:

error page

This is what I have in my Azure AD Redirect URL"

https://laravelproject.laraapps.net/login/azure/callback

How do I resolve it?

Thank you.


Solution

  • You can find the real redirect_uri in the authorize endpoint just before you input your credentials.

    Something like https://login.microsoftonline.com/{tenant}/oauth2/authorize? client_id=6731de76-14a6-49ae-97bc-6eba6914391e &response_type=code &redirect_uri=http%3A%2F%2Flocalhost%3A12345 &response_mode=query &resource=https%3A%2F%2Fservice.contoso.com%2F &state=12345

    enter image description here

    Make sure this redirect_uri is the same as the one in Azure portal.

    enter image description here