Asp.net

HttpContext 不包含 SignOutAsync 的定義

  • February 27, 2018

我正在嘗試編寫一個自定義身份驗證管理器,該管理器將在我的 ASP .Net Core 2.x 應用程序中處理使用者登錄和註銷以及許多其他內容,但我首先被困住了。

我已經嘗試過這篇 Microsoft 文章中建議的方式,但是當我嘗試實現登錄時,它會顯示HttpContext does not contain a definition for SignOutAsync錯誤。我有文章中建議的所有參考資料:

public async void SignIn(HttpContext httpContext, UserDbModel user, bool isPersistent = false)
       {
           ClaimsIdentity identity = new ClaimsIdentity(this.GetUserClaims(user), CookieAuthenticationDefaults.AuthenticationScheme);
           ClaimsPrincipal principal = new ClaimsPrincipal(identity);
           await HttpContext.SignInAsync(IdentityConstants.ApplicationScheme);
       }

以下程式碼有效,但已過時:

await HttpContext.Authentication.SignOutAsync(...)

類中的引用:

using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authentication;

這裡缺少什麼?也許這些擴展方法在新版本中被刪除了,如果是這樣……我如何以正確的方式實現身份驗證?

HttpContext應該是對您的欄位的引用,Controller並且您不是在引用 a/the type HttpContext。如果不是這種情況,那麼這就是您的問題的原因,請更改您的程式碼以使用欄位/變數而不是類型。

因此,如果使用欄位名稱,httpContext則呼叫擴展方法是通過引用實例上的方法而不是類型來完成的,因為實例也作為擴展方法的第一個參數傳入。

await httpContext.SignInAsync(IdentityConstants.ApplicationScheme);

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