I'm trying to implement a command line app using my MyApp.Core
, MyApp.Application
and MyApp.EntityFramework
modules that are working perfectly with a MyApp.Web
module.
The problem with the MyApp.Console
app is the login: once I execute the login, AbpSession
values are still null
, so when I invoke a service protected by any permission, it returns me that the user is not authenticated.
Here is the code:
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?
Inspired by the Aaron's answer I decided to create my IAbpSession
implementation.
For doing it I simply cloned the TestAbpSession
available here in a dirty MyAbpSession
. Then I configured it as default IAbpSession
in MyApp.Console module.
Here it is the code:
[DependsOn(
typeof(MyAppApplicationModule),
typeof(MyAppDataModule))]
public class MyConsoleAppModule : AbpModule {
public override void PreInitialize() {
// set my IAbpSession implementation
Configuration.IocManager.RegisterIfNot<IAbpSession, MyAbpSession>(DependencyLifeStyle.Singleton);
}
public override void Initialize() {
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
}
}
And in the application class:
public class AppMain : ITransientDependency {
public MyAbpSession AbpSession { get; set; }
public AppMain() {
// What to set here?
//AbpSession = NullAbpSession.Instance;
}
// code to login
private async Task Login(userName: string, password: string, tenantName: string) {
var result = await _logInManager.LoginAsync(username, password, tenantName, false);
if (result.Result != AbpLoginResultType.Success) throw new AbpAuthorizationException(result.Result.ToString());
AbpSession.TenantId = result.User.TenantId;
AbpSession.UserId = result.User.Id;
}
public async Task Run(AppOptions options) {
await Login(options.userName, options.password, options.TenantName)
// my code ...
}
}