c# - GetExternalLoginInfoAsync 始终返回 null Azure AD
问题描述
GetExternalLoginInfoAsync 始终返回 null。我尝试将 Azure AD 集成到 Identity 中。
例如,我可以通过硬编码添加 loginProvider 吗?
我的启动:
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<IdentityContext>()
.AddDefaultTokenProviders();
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));
services.AddControllersWithViews();
services.AddRazorPages()
.AddMicrosoftIdentityUI();
这是简短的列表,我也使用自定义中间件。
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
我通过按钮点击来挑战
[HttpGet]
public ChallengeResult ExternalSignIn()
{
var redirectUrl = Url.Action(nameof(ExternalLoginCallback));
var properties = _signInManager.ConfigureExternalAuthenticationProperties
(OpenIdConnectDefaults.AuthenticationScheme, redirectUrl);
return new ChallengeResult(OpenIdConnectDefaults.AuthenticationScheme, properties);
}
挑战称之为:
[HttpGet]
public async Task<IActionResult> ExternalLoginCallback()
{
//info is null
var info = await _signInManager.GetExternalLoginInfoAsync().ConfigureAwait(false);
var result = await _signInManager.ExternalLoginSignInAsync(
info.LoginProvider,
info.ProviderKey,
isPersistent: false,
bypassTwoFactor: false)
.ConfigureAwait(false);
//other code
}
解决方案
services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme,
options => options.SignInScheme = IdentityConstants.ExternalScheme);
推荐阅读
- c++ - 如何使用共享数据同步线程С++
- python - 批处理文件使用 sys.argv[] 执行 Python 脚本
- javascript - Discord.js 异步和等待问题
- sql - 重新格式化一张表以将其合并到另一张表中
- java - 这是 Akka 角色的正确用例吗?
- flutter - 将意图共享到应用程序时如何导航到另一个页面?
- json - 从 URL 获取 JSON 数据响应到 Spring Boot 项目
- rust - 为什么会出现这个错误?我们不能在函数括号内使用格式宏吗?
- c - freeRTOS 进程切换
- javascript - 使用 NuxtLink 重新运行外部 JavaScript