[我正在尝试使用MyApp.Core
,MyApp.Application
和MyApp.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?
ClaimsAbpSession
,IAbpSession
的默认实现,从当前用户的委托人的声明中获取会话属性,通常在调用_signInManager.SignInAsync(...)
之后在(web)响应中设置这些属性。
要在TenantId
之后获得UserId
和_logInManager.LoginAsync(...)
,可以使用loginResult.User.TenantId
和loginResult.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