无法立即获取当前用户身份详细信息

时间:2017-11-26 17:31:44

标签: c# asp.net-mvc asp.net-identity

在MVC5项目中,我正在尝试在调用User.Identity方法后立即获取AuthenticationManager.SignIn详细信息。那时它立即提供空值。但在重定向到另一个actoer 1或2次后,它提供了预期的细节。

以下是SignInAsync

中的代码
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); 

从这里开始,我通过服务类从控制器动作调用该方法。

if (await accountService.LoginAsync(model))
{
    var user = User.Identity.Name; //Here value is showing ""
}

但是在重定向到另一个动作之后,这个相同的代码返回当前登录的用户名。有什么理由这样做吗?

0 个答案:

没有答案
相关问题