0

I have a backend website (ASP.NET MVC) that has his own tenantId (ASP.NET Identity) and this website can create users for other websites with their own tenantId's (all Tenant's are in the same database). The problem is that when i use the Create method from ApplicatoinUserManager, the ApplicationUser even if i put the TenantId corresponding to the website that the user belongs, the user is created with the TenantId of my backend website. Why this is happening? Here is the register code for the users:

internal IdentityResult Register(string username, string email, string password, out ApplicationUser user, int? tenantId = null) {
            user = new ApplicationUser {
                UserName = username,
                Email = email,
                IsAnonymous = false,
                Comment = string.Empty,
                IsApproved = true,
                IsLockedOut = false,
                FailedPasswordAnswerAttemptCount = 0,
                FailedPasswordAttemptCount = 0,
            };
            if (tenantId != null)
                user.TenantId = tenantId.Value;

            return userManager.Create(user, password);
        }

Update

This is the entire AuthenticationLogic class that i use to create the user, nothings here sets the user tenantid to 6:

public class AuthenticationLogic {
        public AuthenticationLogic(IOwinContext owinContext) {
            userManager = owinContext.GetUserManager<ApplicationUserManager>();
        }

        private ApplicationUserManager userManager;


        internal IdentityResult Register(string username, string email, string password, out ApplicationUser user, int? tenantId = null) {
            user = new ApplicationUser {
                UserName = username,
                Email = email,
                IsAnonymous = false,
                Comment = string.Empty,
                IsApproved = true,
                IsLockedOut = false,
                FailedPasswordAnswerAttemptCount = 0,
                FailedPasswordAttemptCount = 0,
            };
            if (tenantId != null)
                user.TenantId = tenantId.Value;

            return userManager.Create(user, password);
        }

    }
Phoenix_uy
  • 3,173
  • 9
  • 53
  • 100
  • It would seem that user.tenantId is a value type (non-nullable) so this defaults to 0, which is likely the same id as your backend website. So, if tenantId is null, it will use the default which is 0. – Erik Funkenbusch Mar 27 '15 at 21:00
  • Actually the backend website TenantId is 6, so the idea is that if i want to register a user for the backend, the tenantId is sent to the method with null and the Create method creates the user with `TenantId = 6` but this happens even if i set the TenantId with other Id different from 6 – Phoenix_uy Mar 27 '15 at 21:02
  • Then you have code somewhere else that sets it to 6... i'd look there. You haven't posted that code here. – Erik Funkenbusch Mar 27 '15 at 21:06
  • The only place that i think it could be setting the tenantId is on the `web.config` of the backend website? the thing is that this variable in the `web.config` is not used for Identity – Phoenix_uy Mar 27 '15 at 21:09
  • You just said that the Create Method sets the id to 6 if it's null.. so that's where you need to look. – Erik Funkenbusch Mar 27 '15 at 21:11
  • Yes.. this is part of the problem.. why is the `Create` method setting 6 even if the TenantId is null? – Phoenix_uy Mar 27 '15 at 21:12

0 Answers0