IdentityServer4 - Login directly from an external

2019-07-07 07:31发布

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?

1条回答
三岁会撩人
2楼-- · 2019-07-07 08:22

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);
}
查看更多
登录 后发表回答