You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Sample documentation on how to authenticate with Azure AD AND Azure B2C in one application .net core 3.x #549
Can you show an example of how to create an application on which employees from an Azure AD can sign-in and with another url users from an Azure B2C instance? I can have them both work separately, but not together.
Reacted by Narayana R, Kevin Scott and Sven Glöckner
changed the title [-][Documentation] [/-][+]Sample documentation on how to authenticate with Azure AD AND Azure B2C in one application .net core 3.x[/+]on Sep 4, 2020
Thank you. I will give it a try. I have no webapi's as given in this example by the way.
An unhandled exception occurred while processing the request. InvalidOperationException: No authentication handler is registered for the scheme 'OpenIdConnect'. The registered schemes are: Bearer, B2CScheme. Did you forget to call AddAuthentication().Add[SomeAuthHandler]("OpenIdConnect",...)?
I modified for now using webapi, but mvc. I get System.InvalidOperationException: 'Scheme already exists: Cookies'
I guess the openconnect is using identical cookie for both azuread and azureb2c. I guess loading azureb2c users into azuread is faster than getting this to work.
` services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration, "AzureAd");
//.EnableTokenAcquisitionToCallDownstreamApi()
//.AddInMemoryTokenCaches();
services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration, "AzureAdB2C", "B2CScheme");
//.EnableTokenAcquisitionToCallDownstreamApi();
// Sign-in users with the Microsoft identity platform
//services.AddMicrosoftIdentityWebAppAuthentication(Configuration);
//services.AddMicrosoftIdentityWebAppAuthentication(Configuration, AzureADB2CDefaults.AuthenticationScheme);
services.Configure(options =>
{
options.TokenValidationParameters = new TokenValidationParameters { NameClaimType = "emails" };
options.ProtocolValidator.NonceLifetime = TimeSpan.FromHours(1);
// Specify the scope by appending all of the scopes requested into one string (separated by a blank space)
options.Scope.Add($"openid profile offline_access"); // {ReadTasksScope} {WriteTasksScope}"
});
services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
I have difficulities understanding how this should work. I have no errors now in the startup, but getting it to work is another thing.
I did not add 4 schemes, only 2?
InvalidOperationException: No authentication handler is registered for the scheme 'AzureB2C'. The registered schemes are: AzureAd, AzureAD, b2c, AzureADB2C. Did you forget to call AddAuthentication().Add[SomeAuthHandler]("AzureB2C",...)?
a class="nav-link text-dark" asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignIn" asp-route-scheme="AzureAd">Sign in ad
a class="nav-link text-dark" asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignIn" asp-route-scheme="AzureB2C">Sign in B2C
I would expect only to do this:
services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAd")
services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAdB2C")
I think the issue lies in the fact that the configuration options for 2 authentication schemes are overwritten and/or only 1 is allowed.
services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAd", "AzureAd", "AzureAdCookies"); services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAdB2C", "AzureAdB2C", "AzureAdB2CCookies");
Above makes 4 schemes, if AzureAd is not defined OpenConnectId is used although it is called 'AzureAd'. This is confusing by the way. In particular as I want to code functional and not in-depth into Azure AD/B2C, it is the start of most application, but not the goal on itself, like I am trying to resolve 2 authentication schemes now.
I am looking at the Woodgrove sample and your code is more or less the same...would be nice to have Woodgrove work with your library as that is exacly what I am looking for.....but with less code like yours.
` [HttpGet()]
public IActionResult SignInAzureAd()
{
var scheme = "AzureAd";
var redirectUrl = Url.Content("~/");
var challenge = new ChallengeResult(
scheme,
new AuthenticationProperties { RedirectUri = redirectUrl });
return challenge;
}
[HttpGet()]
public IActionResult SignInAzureAdB2C()
{
var scheme = "AzureAdB2C";
var redirectUrl = Url.Content("~/");
var challenge = new ChallengeResult(
scheme,
new AuthenticationProperties { RedirectUri = redirectUrl });
return challenge;
}`
I confirm something is wrong in your code. Not exactly sure where as I have no net 5.0 to add a default asp .netcore next to your code to see how that goes. If I add this piece of code from Woodgrove sample I get in my accountcontrollers 2 different (but still erronous) calls to Azure AD and Azure B2C, but as setup is not 100% both do not exactly work.
I have it working with Woodgrove as example. Not using this library as I believe the MicrosoftIdentityOptions and scheme can only be one instead of multiple. Hope you get the idea so I can replace all this code with this library in future. 404 and cookie expiration to be investigated now.
However, it's not working on my side. I'd like to be able that users of both Azure AD and Azure AD B2C are able to login to my application.
I think my requirements are different than what is shown in the sample code.
Contengo Ltd (@Contengo) Blazor Server is the same as plain ASP.NET Core and Razor pages. Just have a look at my sample above. I have it working with a Blazor Server app.
Contengo Ltd (@Contengo) Nice that it's working now at your side.
The separation of B2B and B2C is by design and absolutely makes sense. Otherwise you get mixed-up environments. The fundamental part of B2C is that it's a stripped-down AAD tenant.
The statement where exception occurs:
var res = await GraphClient.Me.Events.Request().GetAsync();
Exception:
Code: generalException
Message: An error occurred sending the request.
InnerException {"IDW10503: Cannot determine the cloud Instance. The provided authentication scheme was ''. Microsoft.Identity.Web inferred 'Cookies' as the authentication scheme. Available authentication schemes are 'Cookies,AzureAd'. See https://aka.ms/id-web/authSchemes. "}
Activity
[-][Documentation] [/-][+]Sample documentation on how to authenticate with Azure AD AND Azure B2C in one application .net core 3.x[/+]jmprieur commentedon Sep 4, 2020
Would the following help, J. Ossewaarde (@UM001) ?
https://github.com/AzureAD/microsoft-identity-web/wiki/web-apis#using-multiple-authentication-schemes
(adapted to Web Apps)
UM001 commentedon Sep 4, 2020
Thank you. I will give it a try. I have no webapi's as given in this example by the way.
An unhandled exception occurred while processing the request. InvalidOperationException: No authentication handler is registered for the scheme 'OpenIdConnect'. The registered schemes are: Bearer, B2CScheme. Did you forget to call AddAuthentication().Add[SomeAuthHandler]("OpenIdConnect",...)?UM001 commentedon Sep 4, 2020
I modified for now using webapi, but mvc. I get System.InvalidOperationException: 'Scheme already exists: Cookies'
I guess the openconnect is using identical cookie for both azuread and azureb2c. I guess loading azureb2c users into azuread is faster than getting this to work.
` services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration, "AzureAd");
//.EnableTokenAcquisitionToCallDownstreamApi()
//.AddInMemoryTokenCaches();
services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration, "AzureAdB2C", "B2CScheme");
//.EnableTokenAcquisitionToCallDownstreamApi();
// Sign-in users with the Microsoft identity platform
//services.AddMicrosoftIdentityWebAppAuthentication(Configuration);
//services.AddMicrosoftIdentityWebAppAuthentication(Configuration, AzureADB2CDefaults.AuthenticationScheme);
services.Configure(options =>
{
options.TokenValidationParameters = new TokenValidationParameters { NameClaimType = "emails" };
options.ProtocolValidator.NonceLifetime = TimeSpan.FromHours(1);
// Specify the scope by appending all of the scopes requested into one string (separated by a blank space)
options.Scope.Add($"openid profile offline_access"); // {ReadTasksScope} {WriteTasksScope}"
});
services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
jmprieur commentedon Sep 4, 2020
J. Ossewaarde (@UM001) : the overrides have default parameters, including the cookie scheme:
microsoft-identity-web/src/Microsoft.Identity.Web/WebAppExtensions/MicrosoftIdentityWebAppAuthenticationBuilderExtensions.cs
Line 43 in 56a8ee3
you could use a different name, and that would work.
UM001 commentedon Sep 4, 2020
I have difficulities understanding how this should work. I have no errors now in the startup, but getting it to work is another thing.
I did not add 4 schemes, only 2?
InvalidOperationException: No authentication handler is registered for the scheme 'AzureB2C'. The registered schemes are: AzureAd, AzureAD, b2c, AzureADB2C. Did you forget to call AddAuthentication().Add[SomeAuthHandler]("AzureB2C",...)?a class="nav-link text-dark" asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignIn" asp-route-scheme="AzureAd">Sign in ad
a class="nav-link text-dark" asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignIn" asp-route-scheme="AzureB2C">Sign in B2C
` services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAd",
AzureADDefaults.AuthenticationScheme,
"AzureAd", false);
I would expect only to do this:
services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAd")
services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAdB2C")
UM001 commentedon Sep 4, 2020
It keeps going to azure b2c, not to azure ad anymore:
` services.AddMicrosoftIdentityWebAppAuthentication(Configuration);
UM001 commentedon Sep 5, 2020
I think the issue lies in the fact that the configuration options for 2 authentication schemes are overwritten and/or only 1 is allowed.
services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAd", "AzureAd", "AzureAdCookies"); services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAdB2C", "AzureAdB2C", "AzureAdB2CCookies");Above makes 4 schemes, if AzureAd is not defined OpenConnectId is used although it is called 'AzureAd'. This is confusing by the way. In particular as I want to code functional and not in-depth into Azure AD/B2C, it is the start of most application, but not the goal on itself, like I am trying to resolve 2 authentication schemes now.
I am looking at the Woodgrove sample and your code is more or less the same...would be nice to have Woodgrove work with your library as that is exacly what I am looking for.....but with less code like yours.
` [HttpGet()]
public IActionResult SignInAzureAd()
{
var scheme = "AzureAd";
var redirectUrl = Url.Content("~/");
var challenge = new ChallengeResult(
scheme,
new AuthenticationProperties { RedirectUri = redirectUrl });
UM001 commentedon Sep 5, 2020
I confirm something is wrong in your code. Not exactly sure where as I have no net 5.0 to add a default asp .netcore next to your code to see how that goes. If I add this piece of code from Woodgrove sample I get in my accountcontrollers 2 different (but still erronous) calls to Azure AD and Azure B2C, but as setup is not 100% both do not exactly work.
` private static void ConfigureAuthentication(IConfiguration configuration, IServiceCollection services)
{
var authenticationBuilder = services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
UM001 commentedon Sep 5, 2020
I have it working with Woodgrove as example. Not using this library as I believe the MicrosoftIdentityOptions and scheme can only be one instead of multiple. Hope you get the idea so I can replace all this code with this library in future. 404 and cookie expiration to be investigated now.
jmprieur commentedon Dec 10, 2020
See also #173
23 remaining items
jmprieur commentedon Jul 7, 2021
Sven Glöckner (@sven5) : this is here: https://github.com/AzureAD/microsoft-identity-web/wiki/multiple-authentication-schemes
sven5 commentedon Jul 7, 2021
Jean-Marc Prieur (@jmprieur) Thanks, I already looked into this.
However, it's not working on my side. I'd like to be able that users of both Azure AD and Azure AD B2C are able to login to my application.
I think my requirements are different than what is shown in the sample code.
jmprieur commentedon Jul 7, 2021
Sven Glöckner (@sven5) : did you see this sample? https://github.com/AzureAD/microsoft-identity-web/tree/master/tests/MultipleAuthSchemes
sven5 commentedon Jul 7, 2021
Jean-Marc Prieur (@jmprieur) Yes, I'm currently trying to find the cause of my issues. Perhaps I have a misconfiguration.
sven5 commentedon Jul 7, 2021
Jean-Marc Prieur (@jmprieur) I've created a minimal repo sample here: https://github.com/sven5/MultipleAuthTest
The
User.Identity.IsAuthenticatedis always false. What am I missing here?jennyf19 Do you have any idea?
Thanks!
edit: I tried several combinations now and couldn't get it working
sven5 commentedon Jul 9, 2021
Update. After lots of trial & error and looking into the source code I've finally found a working solution.
The key is to use Cookie authentication as default and passing the value of
nullto thecookieSchemeparameter.My code now looks like:
jmprieur commentedon Jul 9, 2021
Thanks for sharing your findings, Sven Glöckner (@sven5).
I'll update the documentation in the wiki
sven5 commentedon Sep 6, 2021
Contengo Ltd (@Contengo) Blazor Server is the same as plain ASP.NET Core and Razor pages. Just have a look at my sample above. I have it working with a Blazor Server app.
sven5 commentedon Sep 7, 2021
Contengo Ltd (@Contengo) For your scenario there is a nice wiki article here.
damienbod commentedon Sep 8, 2021
Hi Contengo Ltd (@Contengo) I did an example for this here:
https://damienbod.com/2021/07/26/securing-asp-net-core-razor-pages-web-apis-with-azure-b2c-external-and-azure-ad-internal-identities/
The APIs need to use the right scheme as well
Greetings Damien
sven5 commentedon Sep 24, 2021
Contengo Ltd (@Contengo) Nice that it's working now at your side.
The separation of B2B and B2C is by design and absolutely makes sense. Otherwise you get mixed-up environments. The fundamental part of B2C is that it's a stripped-down AAD tenant.
For application developers, there also is another way to go: You could use B2C even for your B2B accounts to allow logging in. But you have to adjust the custom policies, which is an extra complex step. You could read more about here: https://docs.microsoft.com/en-us/azure/active-directory-b2c/identity-provider-azure-ad-single-tenant?pivots=b2c-custom-policy
babarkhalid commentedon Dec 29, 2023
Could someone please talk about this issue?
My Startup.cs authentication code:
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
{
options.LoginPath = "/LoginPage";
options.ExpireTimeSpan = new TimeSpan(7, 0, 0, 0);
});
services.AddAuthentication()
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"), Microsoft.Identity.Web.Constants.AzureAd, null)
.EnableTokenAcquisitionToCallDownstreamApi(Configuration.GetValue("DownstreamApi:Scopes")?.Split(' '))
.AddMicrosoftGraph(Configuration.GetSection("DownstreamApi"))
.AddInMemoryTokenCaches();
The statement where exception occurs:
var res = await GraphClient.Me.Events.Request().GetAsync();
Exception:
Code: generalException
Message: An error occurred sending the request.
InnerException {"IDW10503: Cannot determine the cloud Instance. The provided authentication scheme was ''. Microsoft.Identity.Web inferred 'Cookies' as the authentication scheme. Available authentication schemes are 'Cookies,AzureAd'. See https://aka.ms/id-web/authSchemes. "}
System.Exception {System.InvalidOperationException}
I really appreciate any help you can provide.