Code: Select all
Startup.cs
Code: Select all
services.AddAuthentication(defaultScheme: AzureADDefaults.AuthenticationScheme).AddAzureAD(options => Configuration.Bind("AzureAd", options));
services.Configure (AzureADDefaults.OpenIdScheme, options =>
{
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProvider = async context =>
{
if (context.Properties.Items.TryGetValue("userId", out var userId))
{
context.ProtocolMessage.LoginHint = userId;
}
await Task.Yield();
},
};
});
< /code>
Login: < /p>
var redirectUrl = Url.Page("/Account/ExternalLogin", pageHandler: "Callback", values: new { returnUrl, area = "Identity" });
var properties = _signInManager.ConfigureExternalAuthenticationProperties(AzureADDefaults.AuthenticationScheme, redirectUrl);
properties.Items["userId"] = Input.Email;
return new ChallengeResult(AzureADDefaults.AuthenticationScheme, properties);
< /code>
Rückruf: < /p>
public async Task OnGetCallbackAsync(string returnUrl = null, string remoteError = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
var info = await _signInManager.GetExternalLoginInfoAsync();
}
< /code>
Das habe ich geändert in: < /p>
Startup.cs
Code: Select all
services.AddAuthentication().AddMicrosoftIdentityWebApp(options =>
{
Configuration.Bind("AzureAd", options);
options.Events ??= new OpenIdConnectEvents();
options.GetClaimsFromUserInfoEndpoint = true;
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProvider = async context =>
{
if (context.Properties.Items.TryGetValue("userId", out var userId))
{
context.ProtocolMessage.LoginHint = userId;
}
await Task.Yield();
},
};
});
< /code>
Login: < /p>
var redirectUrl = Url.Page("/Account/ExternalLogin", pageHandler: "Callback", values: new { returnUrl, area = "Identity" });
var properties = _signInManager.ConfigureExternalAuthenticationProperties(OpenIdConnectDefaults.AuthenticationScheme, redirectUrl);
properties.Items["userId"] = Input.Email;
return new ChallengeResult(OpenIdConnectDefaults.AuthenticationScheme, properties);
< /code>
Rückruf bleibt gleich. Es fühlt sich an, als wäre ich nah, aber vielleicht unterscheidet sich die Rückgabedaten irgendwie zwischen der Verwendung von Azuread OpenID (AzureADDefaults.OpenIdScheme
Code: Select all
OpenIdConnectDefaults.AuthenticationScheme
Code: Select all
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration);