Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IdentityServer4 - Login directly from an external provider

I've implemented the option to login from Azure AD. And the client type I'm using is Hybrid. So now, when a user enters a restricted control on my application, he is being redirected to a login page (on the IdentityServer application site) where he can either enter a username and password or login with an Azure AD account.

What I want to be able to do is skip the login page and redirect the user directly to the MS AD login page. Meaning, the user will click a "Login" link on the website, and that will lead him to the Azure AD login page. Once he successful logged in, he will be redirected back to my application (basically the same flow, just save that extra step of entering IdentityServer login page and clicking the external login button).

Is this possible?

like image 646
developer82 Avatar asked Aug 10 '17 04:08

developer82


1 Answers

In the client options, try setting EnableLocalLogin to false. From the docs:

EnableLocalLogin

Specifies if this client can use local accounts, or external IdPs only. Defaults to true.

I'm using Asp.Net Core Identity as well, and I set the AccountsController to bypass the local page if EnableLocalLogin is false and there is only one external provider, or if the idP is explicitly set in the request.

[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> Login(string returnUrl = null)
{
    // Clear the existing external cookie to ensure a clean login process
    await HttpContext.Authentication.SignOutAsync(_externalCookieScheme);

    var context = await _interaction.GetAuthorizationContextAsync(returnUrl);
    if (context?.IdP != null)
    {
        // if IdP is passed, then bypass showing the login screen
        return ExternalLogin(context.IdP, returnUrl);
    }

    var vm = await BuildLoginViewModelAsync(returnUrl, context);

    if (vm.EnableLocalLogin == false && vm.ExternalProviders.Count() == 1)
    {
        // only one option for logging in
        return ExternalLogin(vm.ExternalProviders.First().AuthenticationScheme, returnUrl);
    }

    return View(vm);
}
like image 93
Alyce Avatar answered Mar 04 '23 10:03

Alyce



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!