繁体   English   中英

GetExternalLoginInfoAsync在ASP.NET Core 2.0中返回null

[英]GetExternalLoginInfoAsync returns null in asp.net core 2.0

我想在aspnet core 2.0中使用OAuth连接到GitHub。 我有以下设置。

Startup.cs:

services.AddDbContext<DbContext>(options =>
            options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection"))
        );

        services.AddIdentity<User, Role>()
        .AddEntityFrameworkStores<DbContext>()
        .AddDefaultTokenProviders();

        services.AddMvc(options => options.OutputFormatters.Add(new HtmlOutputFormatter()));
        services.AddAntiforgery(x => x.HeaderName = "X-XSRF-TOKEN");

        services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        })
        .AddCookie()
        .AddGitHub(options =>
        {
            options.ClientId = "xxxxxxxxxx";
            options.ClientSecret = "xxxxxxxxxxxxxxx";
            options.CallbackPath = new PathString("/auth/callback/GitHub");
        })

控制器:

[HttpGet("{provider}")]
    public IActionResult Index(string provider)
    {
        return Challenge(new AuthenticationProperties() { RedirectUri = Url.Action("CallBack", "Auth") }, provider);
    }

[HttpGet("callback")]
    public async Task<IActionResult> CallBack()
    {
        var info = await _signInManager.GetExternalLoginInfoAsync(); // <== this is always NULL
        return Redirect("~/");
    }

身份验证流程正确完成,没有错误。 但是,线

var info = await _signInManager.GetExternalLoginInfoAsync();

总是产生null。

网络上散布着一些线索,就像这样 ,似乎没有任何东西会刮擦我的痒。

有什么想法吗? 我在某处缺少设置吗? 大概...

谢谢!

与Identityserver4一起使用时,在代码的以下部分IdentityConstants.ExternalScheme不匹配,因此“ auth”为null

但是,将其更改为idsrv.external可以继续进行。

 public async Task<ExternalLoginInfo> GetExternalLoginInfoAsync(string expectedXsrf = null)
    {
        var auth = await HttpContext.AuthenticateAsync(IdentityConstants.ExternalScheme);
        var items = auth?.Properties?.Items;
        if (auth?.Principal == null || items == null || !items.ContainsKey("LoginProviderKey"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM