Asp.Net Core Identity With MultiTenant

dotnet ef migrations add  UserNameUniqueFalse   --context UsersContext

dotnet ef database update --context UsersContext

-----------

dotnet-ef migrations add First --project IdentityDemo1

dotnet-ef database update --project IdentityDemo1

----------



Continue

https://stackoverflow.com/questions/50786399/override-validateasync-in-uservalidator-cs-for-net-core-identity-2-1

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddUserManager<CustomUserManager<ApplicationUser>>()

And finally inject this class

            services.AddTransient<ICustomUserValidator<ApplicationUser>, CustomUserValidator<ApplicationUser>>();

First let me explain the problem

The reason is why the Identity library injects the validate user class that uses the default library, which is UserValidator.

The solution is only to inject a CustomUserValidator. What happens is that if it is injected into the normal implementation, what it does is that it adds 2 UserValidator, the first is the default by the identity library and the second would be the one that you implemented the CustomUserIdentity.

Then to inject only the CustomUserIdentity you must create a new CustomUserManager to be able to inject the new ICustomUserValidator so that it does not take the one that is by default IUserValidator.

This is my solution:

This is the interface ICustomUserValidator

    public interface ICustomUserValidator<TUser> : IUserValidator<TUser> where TUser : ApplicationUser
{
}

And the implementation of the class

public class CustomUserValidator<TUser> : UserValidator<TUser>, ICustomUserValidator<TUser>
    where TUser : ApplicationUser
{

    public async Task<IdentityResult> ValidateAsync(UserManager<TUser> manager, TUser user)
    {
        //Some Code
    }

    private async Task ValidateUserName(UserManager<TUser> manager, TUser user, ICollection<IdentityError> errors)
    {
        //Some Code
    }
}

And this one for the CustomUserManager

    public class CustomUserManager<TUser> : UserManager<TUser>
            where TUser : ApplicationUser
{
    public CustomUserManager(IUserStore<TUser> store, IOptions<IdentityOptions> optionsAccessor,
        IPasswordHasher<TUser> passwordHasher, IEnumerable<ICustomUserValidator<TUser>> userValidators,
        IEnumerable<IPasswordValidator<TUser>> passwordValidators, ILookupNormalizer keyNormalizer,
        IdentityErrorDescriber errors, IServiceProvider tokenProviders,
        ILogger<UserManager<TUser>> logger)
        : base(
            store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors,
            tokenProviders, logger)
    {
    }
}

Notice that instead of IUserValidator, I put ICustomUserValidator

In the Startup class you have to inject the new class:

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddUserManager<CustomUserManager<ApplicationUser>>()

And finally inject this class

            services.AddTransient<ICustomUserValidator<ApplicationUser>, CustomUserValidator


There may be only 2 option 

createasync

validate method 

we will  try to override or extend that 

--------------------------------

https://thecodeblogger.com/2020/01/23/adding-asp-net-core-identity-to-web-api-project/

enable-migrations -ContextTypeName


https://stackoverflow.com/a/47635205/8641086

https://andrewlock.net/creating-custom-password-validators-for-asp-net-core-identity-2/

looks like we've implement UserValidator and need to setup with startup.cs 

and in applicationdbcontext class need to do some changes 




 * EF Core uses a metadata model to describe how the application's entity types are mapped to the underlying database

User Claims

A set of statements (or Claims) about the user that represent the user's identity. Can enable greater expression of the user's identity than can be achieved through roles.


There may be only 2 option 

createasync

validate method 

we will  try to override or extend that 



 UserManager<TUser> -- > CreateAsync

https://stackoverflow.com/questions/29094063/how-to-allow-user-to-register-with-duplicate-username-using-identity-framework-1

  • IUserStore
    The IUserStore<TUser> interface is the only interface you must implement in the user store. It defines methods for creating, updating, deleting, and retrieving users.


---------------------------


IdentityDemo with Sql


D:\Practice Projects\@Core\IdentityDemo1

References 

https://medium.com/@zahidcakici/multitenancy-and-finbukcle-in-net-f1d5e7e5f1bf

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.iuserstore-1.createasync?view=aspnetcore-7.0#microsoft-aspnetcore-identity-iuserstore-1-createasync(-0-system-threading-cancellationtoken)









custom-identity-columns-in-aspdotnet-core

https://www.freecodespot.com/blog/custom-identity-columns-in-aspdotnet-core/

change the migration file if table name was changed 

migrationBuilder.CreateTable(
                name: "user",
                 schema: "public",

- first change the migration file as require then run update - database command  

dotnet ef migrations add IdentityDbUser  --context UsersContext

dotnet ef database update --context UsersContext


to store 2 username in identity & extra column

https://www.scottbrady91.com/aspnet-identity/quick-and-easy-aspnet-identity-multitenancy


Proj  - D:\Practice Projects\@Core\IdentityDemo1


dotnet-ef migrations add First --project IdentityDemo1

dotnet-ef database update --project IdentityDemo1

https://docs.google.com/spreadsheets/d/1idbCiBQkWugFwKJFNi8W0llEd-uZeIUcZma9f5XtT_g/edit#gid=1161891927

Register flow in identity





ContryDBParent PortalManagement portal
FranklinNCNC069https://test.osmsinc.com/FranklinNChttps://test.osmsinc.com/FranklinNCPortal
AscensionLALA005https://test.osmsinc.com/AscensionLAhttps://test.osmsinc.com/AscensionLAPortal
VolusiaFL127https://test.osmsinc.com/Volusiahttps://test.osmsinc.com/VolusiaPortal
MonroeFLFL087https://test.osmsinc.com/MonroeFLhttps://test.osmsinc.com/MonroeFLPortal
WakeNCNC183https://test.osmsinc.com/WakeNChttps://test.osmsinc.com/WakeNCPortal
HenricoVAVA087https://test.osmsinc.com/HenricoVAhttps://test.osmsinc.com/HenricoVAPortal
SumnerTN165https://test.osmsinc.com/Sumnerhttps://test.osmsinc.com/SumnerPortal
AtlantaGA804https://test.osmsinc.com/Atlantahttps://test.osmsinc.com/AtlantaPortal
MDCPSFL086https://test.osmsinc.com/MDCPShttps://test.osmsinc.com/MDCPSPortal
BrowardFL011
KnoxTNTN093

To Debug

Stop IIs

Debug option from vscode 



Existing DB authentication in osp management portal 

select top 1 *  from [dbo].[aspnet_Applications]

ApplicationName

LoweredApplicationName

ApplicationId

Description

select top 1 *  from [dbo].[Users]

UserID

Username

FirstName

LastName

IsSuperUser

AffiliateId

Email

DisplayName

UpdatePassword

select top 1 *  from [dbo].[aspnet_Users]

ApplicationId

UserId

Password

PasswordFormat

PasswordSalt

MobilePIN

Email

LoweredEmail

PasswordQuestion

PasswordAnswer

IsApproved

IsLockedOut

CreateDate

LastLoginDate

LastPasswordChangedDate

LastLockoutDate

FailedPasswordAttemptCount

FailedPasswordAttemptWindowStart

FailedPasswordAnswerAttemptCount

FailedPasswordAnswerAttemptWindowStart

Comment

select top 1 *  from  [dbo].[aspnet_Membership]

ApplicationId

UserId

Password

PasswordFormat

PasswordSalt

MobilePIN

Email

LoweredEmail

PasswordQuestion

PasswordAnswer

IsApproved

IsLockedOut

CreateDate

LastLoginDate

LastPasswordChangedDate

LastLockoutDate

FailedPasswordAttemptCount

FailedPasswordAttemptWindowStart

FailedPasswordAnswerAttemptCount

FailedPasswordAnswerAttemptWindowStart

Comment

select top 1 *  from [dbo].[Roles]

RoleID

PortalID

RoleName

Description

ServiceFee

BillingFrequency

TrialPeriod

TrialFrequency

BillingPeriod

TrialFee

IsPublic

AutoAssignment

RoleGroupID

RSVPCode

IconFile

select top 1 *  from [dbo].[UserRoles]

UserRoleID

UserID

RoleID

ExpiryDate

IsTrialUsed

EffectiveDate

--assigned list of schools





Comments

Popular Posts