Base solution for your next web application

Activities of "sbenfares"

Hello,

I’m trying to add 2 areas to my project, i would like to properly redirect users on login. Each user have a specific role, depending on his role he will be redirected to the good area.

What is your advice to handle this properly ?

I’m modifying AccountController and HomeController with a GetAppHomeUrl method which use a User.IsInRole verification.

My problem is with the NormalizeReturnUrl function and the UserManager.UserIsInRole verification which is not working. The IsInRole always return false for my users, but return true for the default admin user.

My users are created in the SeedHelper and roles are set with the context : __context.UserRoles.Add(new UserRole(tenantId, user.Id, role.Id));

When i create the users in SeedHelper, should i use UserManager.AddToRoleAsync to make IsInRoleAsync work ? If yes, should i inject UserManager in the SeedHelper ?

Thanks

PS : Multytenancy disabled.

Hi there,

I'm new to ANZ, i've created my first project with MultyTenancy disabled because i don't need it.

If i understand correctly, with MultiTenancy off i still have a default tenantam i right ?

Is it possible to login as host with MultiTenancy off ? If there is a default tenant can i manage it with an host account ?

I tried to log with host user with MT off (using tenancyName empty on login as explained here : [https://forum.aspnetboilerplate.com/viewtopic.php?f=5&t=11035])) but i always have a InvalidUserNameOrEmailAddress error.

Thanks for your help.

Thanks for your answer !

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.

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:

Question

Hi,

I need to handle 2 (or more) types of Users in my application (Example Teacher & Students) Each user wil have common properties (defined in User Table) and have specific fields.

What is the best approach for integrate this with ANZ ?

PS : In my previous application i used Asp.net Identity 2.0 table AspNetUser and Created 2 specific tables : Student & Teacher. On AspNetUser i created a nullable Foreign Key to Student and Teacher table. I followed this recommandation : https://stackoverflow.com/questions/27059017/creating-inheritance-users-from-base-asp-net-identity-user

Then to retrieve Student data i used User.Student.Name

Is that a good practice for you ? Should i use the same method for ANZ and AbpUsers Table ? How can i add StudentId and TeacherId in AbpSession in ANZ ?

Thanks !

I would like to get FirstName and LastName of my users in my chstml views.

With asp.net identity we can have @User.identity.Name but only the name is exposed.

What is the cleanest method to get User.FirstName & Lastname (& other properties) in ANZ ?

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

Hi,

I would like to center the div/canvas displayed in the modal when changing the profile picture in the ChangeProfilePicture Modal.

I can't figure how to do this, what element should i modify in Jcrop or in the modal...

Thanks!

PS : You can see the capture attached

Capture.PNG

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.

Showing 1 to 10 of 53 entries