ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#-AspNet核心身份GetExternalLoginInfoAsync始终为空

2019-12-11 00:07:16  阅读:510  来源: 互联网

标签:identityserver4 net-core asp-net-identity entity-framework-core c


我使用IdentityServer4和ASP .NET Core Identity的组合来创建联合的登录页面.我使用的外部提供程序之一是通过Open ID Connect协议的Azure Active Directory.我还为所有数据存储集成了EntityFrameworkCore.

在使用身份验证对初始网站进行脚手架之后,将适当的服务添加到Startup.cs文件中.

services.AddOidcStateDataFormatterCache();
// Some DI registrations
services.Configure<CookiePolicyOptions>(options =>
{
    options.CheckConsentNeeded = context => true;
    options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<MyDbContext>();
services.AddMvc().AddRazorPages(options => /* Some options */);
services.AddIdentityServer(options =>
    options.Events.RaiseErrorEvents = true;
    options.Events.RaiseFailureEvents = true;
    options.Events.RaiseInformationEvents = true;
    options.Events.RaiseSuccessEvents = true;
)
.AddConfigurationStore(options => /* Set up DbContext */)
.AddOperationStore(options => /* Set up DbContext */)
.AddAspNetIdentity<MyAppUser>();
services.AddAuthentication().AddOpenIdConnect(options =>
{
    // Does not bind itself for mysterious reasons
    Configuration.GetSection("OpenIdConect").Bind(options)
});

我决定,如果我在我的appsettings.json文件中进行了大部分身份验证设置,它将看起来更好.

{
    "OpenIdConnect": {
        "ClientId": "<my_client_id>",
        "Authority:" "https://login.microsoft.com/<my_tenant_id>",
        "PostLogoutRedirectUri": "http://localhost:5000/MyApp/Account",
        "CallbackPath": "/signin-oidc",
        "ResponseType": "code id_token",
        "Scope": "openid profile email",
        "SignInScheme": "idsrv.external",
        "AutomaticAuthenticate": "false",
        "AutomaticChallenge": "false",
        "RequireHttpsMetadata": "true"
     }
}

一切正常,我的Open ID Connect登录提供程序自动出现在登录页面上.砸!尝试使用它时,出现一个错误:加载外部登录信息时出错.最终在脚手架剃刀页面上显示
Areas / Identity / Pages / Account / ExternalLogin.cshtml.cs:72包含以下代码:

var info = await _signInManager.GetExternalLoginInfoAsync();
if (info == null)
{
    ErrorMessage = "Error loading external login information.";
    return RedirectToPage("./Login", new { ReturnUrl = returnUrl });
}

info始终解析为null.我怀疑这是我的Startup.cs中某个配置问题,或者是没有任何文档值得一提的魔术.

解决方法:

读完我的大脑后,我决定重读the IdentityServer4 documentation about external identity providers.

这引起了我(疲倦)的注意:

Given that this is such a common practise, IdentityServer registers a cookie handler specifically for this external provider workflow. The scheme is represented via the
IdentityServerConstants.ExternalCookieAuthenticationScheme constant.

If you were to use our external cookie handler, then for the SignInScheme [in your OpenIdConnect service setup] you’d assign the value to be the IdentityServerConstants.ExternalCookieAuthenticationScheme constant

好吧,我想我没有使用他们的外部cookie处理程序.可以肯定的是,我让ASP.NET Core Identity做到了这一点.我决定在我的appsettings.json中取消这一行:

"SignInScheme": "idsrv.external"

而且,我可以使用Active Directory凭据登录.通过阅读GetExternalLoginInfoAsync的实际源代码,我看到他们正在寻找一个非常特定的cookie,而不是“ idsrc.external”.我需要将其保留为默认值.

标签:identityserver4,net-core,asp-net-identity,entity-framework-core,c
来源: https://codeday.me/bug/20191210/2105048.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有