Asp.net-Mvc

MVC 5 具有身份驗證模式的外部身份驗證=表單

  • December 28, 2017

我正在按照本教程創建一個帶有外部身份驗證的簡單 MVC 5 應用程序。它工作正常,但是,如果我將其更改authentication mode="None"authentication mode="Forms"它會停止工作。

我在以下情況下為空:

await HttpContext.GetOwinContext().Authentication.GetExternalLoginInfoAsync()

我正在閱讀很多關於在重定向時抑制 FormsAuthentication 的內容。我不知道這是否是正確的路徑,但我嘗試安裝這個nuget 數據包,問題仍然存在。

那麼,為什麼每次更改身份驗證模式時我都會得到 null 呢?

Response.SuppressFormsAuthenticationRedirect = true通過添加到ChallengeResult類中,我能夠得到這個工作(OWIN 和 FormsAuthentication) 。

如果您按照教程進行操作,以下是程式碼:

public class ChallengeResult : HttpUnauthorizedResult
{
   public ChallengeResult(string provider, string redirectUri)
       : this(provider, redirectUri, null)
   {
   }

   public ChallengeResult(string provider, string redirectUri, string userId)
   {
       LoginProvider = provider;
       RedirectUri = redirectUri;
       UserId = userId;
   }

   public string LoginProvider { get; set; }
   public string RedirectUri { get; set; }
   public string UserId { get; set; }

   public override void ExecuteResult(ControllerContext context)
   {
       // this line did the trick
       context.RequestContext.HttpContext.Response.SuppressFormsAuthenticationRedirect = true;

       var properties = new AuthenticationProperties() { RedirectUri = RedirectUri };
       if (UserId != null)
       {
           properties.Dictionary[XsrfKey] = UserId;
       }

       context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
   }
}

引用自:https://stackoverflow.com/questions/19893718