ASP NET Boilerplate,登录后ABPSession userId为空

问题描述 投票:0回答:1

[我正在尝试使用MyApp.CoreMyApp.ApplicationMyApp.EntityFramework模块与MyApp.Web模块完美配合来实现命令行应用程序。

MyApp.Console应用程序存在的问题是登录名:执行登录后,AbpSession值仍为null,因此,当我调用受任何权限保护的服务时,它将返回我未经身份验证的用户。

这里是代码:

var result = await _logInManager.LoginAsync("myuser", "mypassword "mytenant", true);
if (result.Result != AbpLoginResultType.Success) throw new AbpAuthorizationException(result.Result.ToString());
// *** WHY AbpSession.TenantId and AbpSession.UserId are still null here?
aspnetboilerplate asp.net-boilerplate
1个回答
0
投票

ClaimsAbpSessionIAbpSession的默认实现,从当前用户的委托人的声明中获取会话属性,通常在调用_signInManager.SignInAsync(...)之后在(web)响应中设置这些属性。

要在TenantId之后获得UserId_logInManager.LoginAsync(...),可以使用loginResult.User.TenantIdloginResult.User.Id

您还可以使用登录用户覆盖当前会话值:

var loginResult = await _logInManager.LoginAsync(...);
if (loginResult.Result != AbpLoginResultType.Success) throw new AbpAuthorizationException(...)
using (AbpSession.Use(loginResult.User.TenantId, loginResult.User.Id))
{
    // AbpSession.TenantId and AbpSession.UserId now return the logged-in user's values.
}

参考:https://aspnetboilerplate.com/Pages/Documents/Abp-Session#overriding-current-session-values

© www.soinside.com 2019 - 2024. All rights reserved.