Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC

Activities of "sbenfares"

Thanks it works !

PS : Awesome support for ANZ :)

Thanks

I used the LoginInformations field in the HeaderViewModel, which contains exactly what i need.

No need for the variable in page, best use with the ViewModel.

Thanks for your help.

The name 'loginInfo' does not exist in the current context

You are right it's working without UserManager, it was a missing tenantId that caused my problem.

:D Thanks !

PS : And yes i learned why we should replace XXXX.Result by await thanks to ([https://stackoverflow.com/questions/24623120/await-on-a-completed-task-same-as-task-result])) :ugeek:

OK so there was 2 problems here :

1 - The _userManager.IsInRole was not working when i created a user in SeedHelper. To correct this i injected UserManager in SeedHelper and pass it to TenantRoleAndUserBuilder.

public static class SeedHelper
    {
       private static UserManager UserManager { get; set; }

        public static void SeedHostDb(IIocResolver iocResolver)
        {
            UserManager = iocResolver.Resolve<UserManager>();
            WithDbContext<PlatformDbContext>(iocResolver, SeedHostDb);
        }

        public static void SeedHostDb(PlatformDbContext context)
        {
            context.SuppressAutoSetTenantId = true;

            //Host seed
            new InitialHostDbBuilder(context).Create();

            //Default tenant seed (in host database).
            new DefaultTenantBuilder(context).Create();
            new TenantRoleAndUserBuilder(context, 1, UserManager).Create();
       }

....

}

Then in TenantRoleAndUserBuilder i've added a UserManager readonly field and get it from the constructor. Then in the CreateUser method, i replaced :

//Assign Admin role to admin user
                _context.UserRoles.Add(new UserRole(_tenantId, adminUser.Id, adminRole.Id));
                _context.SaveChanges();

by

//Assign role to user
            _userManager.AddToRoleAsync(user, role.Name).Wait();

Then the _userManager.IsInRole is working now in the AccountController for my users created in SeedHelper.

2 - The second problem was to handle properly the url changes depending on the roles of users.

In the home controller in the Index method i've done this :

var homeUrl = HomeUrlForUser();

            return AbpSession.UserId.HasValue ? 
                RedirectToAction("Index", "Home", new { area = homeUrl }) : 
                RedirectToAction("Login", "Account");

Then i created a HomeUrlForUser method :

private string HomeUrlForUser()
        {
            if (User.IsInRole(StaticRoleNames.Tenants.Candidat)) return "Candidat";
            return User.IsInRole(StaticRoleNames.Tenants.Employeur) ? "Employeur" : "Admin";
        }

In the account controller i've created a method :

private string CheckReturnUrlForRole(string returnUrl, string userNameOrEmailAddress)
        {
            var user = _userManager.FindByNameOrEmailAsync(userNameOrEmailAddress).Result;
            var isEmployeur = _userManager.IsInRoleAsync(user, StaticRoleNames.Tenants.Employeur).Result;
            var isCandidat = _userManager.IsInRoleAsync(user, StaticRoleNames.Tenants.Candidat).Result;

            if (isEmployeur) return returnUrl.Replace("Admin", "Employeur");
            return isCandidat ? returnUrl.Replace("Admin", "Candidat") : returnUrl;
        }

And then in the login method add a call to this CheckReturnUrlForRole function just after GetLoginResultAsync call :

var loginResult = await GetLoginResultAsync(loginModel.UsernameOrEmailAddress, loginModel.Password, GetTenancyNameOrNull());
returnUrl = CheckReturnUrlForRole(returnUrl, loginModel.UsernameOrEmailAddress);

Don't know if this is optimal but it works, and i hope it will help someone having same needs.

Thanks for your answer !

Showing 21 to 27 of 27 entries