How to use both Azure AD authentication and Identity on ASP.NET Core 3?

后端 未结 1 1050
难免孤独
难免孤独 2021-02-04 14:36

The web application should allow internal employees with AD accounts to authenticate in the app using Azure AD Authentication. External users should be able to register and sign

1条回答
  •  无人共我
    2021-02-04 14:59

    If using ASP.NET Core Identity with Azure AD login , you can set CookieSchemeName to Identity.External so that asp.net core identity can get the external user profile from external identity provider , and create a local user associated with external user :

    In appsettings.json :

    "AzureAd": {
        "Instance": "https://login.microsoftonline.com/",
        "Domain": "peterpad.onmicrosoft.com",
        "TenantId": "cb1c3f2e-a2dd-4fde-bf8f-f75ab18b21ac",
        "ClientId": "0c0ec562-a9bb-4722-b615-6dcbdc646326",
        "CallbackPath": "/signin-oidc",
        "CookieSchemeName": "Identity.External"
    },
    

    Then if you want to challenge Azure AD login in MVC controller , you should provide the scheme name , config redirect url after authentication to Identity/Account/ExternalLoginand Callbackhandler , in that handler asp.net core identity will let your enter username and create a local user :

    [HttpGet("internal-signin")]
    public ChallengeResult InternalSignIn(string returnUrl = "/") 
    {
        var redirectUrl = Url.Page("/Account/ExternalLogin", pageHandler: "Callback", values: new { returnUrl , area = "Identity" });
        var properties = _signInManager.ConfigureExternalAuthenticationProperties(AzureADDefaults.AuthenticationScheme, redirectUrl);
        return new ChallengeResult(AzureADDefaults.AuthenticationScheme, properties);
    }
    

    0 讨论(0)
提交回复
热议问题