Asp.net-Mvc-5

登錄成功後,如何在控制器中獲取 Identity UserID?

  • July 2, 2015

我正在使用 Identity v2 和 MVC 5 進行外部登錄。

在我的外部登錄回調函式中,我使用

var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);

然後有一個開關result,我需要在這種success情況下訪問使用者的ID,但User.Identity.GetUserId();在這裡給我null。

在這種情況下如何訪問使用者 ID?

要在成功登錄後立即獲取有關已登錄身份的索賠數據和其他資訊,而無需訪問數據庫,只需訪問signinManager.AuthenticationManager.AuthenticationResponseGrant.Identity.

例如,

switch (result) { case SignInStatus.Success: var userId = signinManager.AuthenticationManager.AuthenticationResponseGrant .Identity.GetUserId(); // ...

您還可以在其他問題的答案中看到它被用於在登錄後立即向身份添加新聲明 -AuthenticationManager.AuthenticationResponseGrant.Identity.AddClaims(freshClaims);

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