簡體   English   中英

ASP.NET Web API Authentication.GetExternalLoginInfoAsync 始終返回 null

[英]ASP.NET Web API Authentication.GetExternalLoginInfoAsync always return null

我有 ASP.NET 5 項目,我正在使用 Web API 來建立外部登錄(用於 Facebook 和 Google)。 就我而言,我有包含以下代碼的 Web API 控制器(不是 MVC 控制器):

[OverrideAuthentication]
[HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)]
[AllowAnonymous]
[Route("ExternalLogin", Name = "ExternalLogin")]
public async Task<IHttpActionResult> GetExternalLogin(string provider, string error = null)
{
    if (error != null)
        return Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error));

    if (!User.Identity.IsAuthenticated)
        return new ChallengeResult(provider, this);

    var exLog = await Authentication.GetExternalLoginInfoAsync();

當移動應用程序調用此方法時,我正確驗證了請求並且屬性User.Identity.IsAuthenticated為真,我可以在調試器中看到用戶是正確的,但問題是exlog變量總是出現空值。

這是Authentication屬性

private IAuthenticationManager Authentication => this.Request.GetOwinContext().Authentication;

我閱讀了堆棧上有關此錯誤的每個問題,但沒有任何幫助,大多數問題都是針對 MVC 控制器的,因此沒有幫助。

例如,這個問題對我沒有幫助,因為它是針對 MVC 的。

更新我正在使用 Owin 4.0

對於所有在評論中要求我回答的人。
其實這個問題還沒有解決,我也沒有找到解決辦法,
我很抱歉這么說。

我發現我不需要從一開始就調用這個方法。
我走了完全不同的路(我不記得如何以及為什么)

只是一個小建議:再次考慮這種情況,並重新考慮是否真的需要調用這個方法。

我希望我能提供更多幫助,我很抱歉。

如果 IsAuthenticated = true,您將收到“NULL”,因為您沒有編寫代碼,那么它應該怎么做? (意味着重定向到任何頁面或其他)。

仔細查看 IsAuthenticated 的 if 條件: if ( !User.Identity.IsAuthenticated )

也許這可以幫助你。

嘗試在 Google 控制台中啟用 Google + API。 還要確保在將密鑰粘貼到代碼后,密鑰沒有在末尾連接一個空格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM