Skip to main content
Version: 1.0.1

Identity

Includes helper structures for performing account operations such as login, logout, register, sending activation mail and more. The library takes advantage of the structure of the open source Microsoft.AspNetCore.Identity.

Usage

Create SampleUser class and inherit from MilvaUser. MilvaUser includes properties that can be found in most projects.


public class SampleUser : MilvaUser<Guid>
{

}

Setup settings and add required services to service collection.


services.AddMilvaIdentity<SampleUser, Guid>()
.WithOptions(opt =>
{
opt.Lockout.DefaultLockoutTimeSpan = new TimeSpan(3, 1, 0);
opt.Lockout.MaxFailedAccessAttempts = 5;
opt.User.RequireUniqueEmail = false;
opt.Password.RequireDigit = false;
opt.Password.RequiredLength = 1;
opt.Password.RequireLowercase = false;
opt.Password.RequireNonAlphanumeric = false;
opt.Password.RequireUppercase = false;
opt.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._";
})
.WithUserManager<MilvaUserManager<SampleUser, Guid>>();


public class AccountService
{
private readonly IMilvaUserManager<SampleUser, Guid> _userManager;
private readonly IGenericRepositoryBase<SampleUser,Guid> _userRepository;

public AccountService(IMilvaUserManager<SampleUser, Guid> userManager, IGenericRepositoryBase<SampleUser,Guid> userRepository)
{
_userManager = userManager;
_userRepository = userRepository;
}

public async Task RegisterAsync(RegisterDTO sampleRegisterDTO)
{
...validation code...

SampleUser userToBeRegister = new()
{
UserName = sampleRegisterDTO.UserName,
PhoneNumber = sampleRegisterDTO.PhoneNumber,
PhoneNumberConfirmed = true,
Email = sampleRegisterDTO.Email,
EmailConfirmed = true,
TwoFactorEnabled = sampleRegisterDTO.TwoFactorEnabled,
LockoutEnabled = sampleRegisterDTO.LockoutEnabled
};

// Validates the "UserName" and "Email" information of the user to be registered according to the settings specified in "Startup.cs".
// Assigns "Normalized..." properties.
// Hashes the password and assigns it to the relevant column.
userToBeSignUp = _userManager.ConfigureForCreate(userToBeRegister, sampleRegisterDTO.Password);

await _userRepository.AddAsync(userToBeRegister);
}

public async Task SendVerificationEmailAsync(string userName)
{
var user = await _userRepository.GetFirstOrDefault(u => u.UserName == userName);

// Generates tokens to be placed in the verification mail to be sent.
var verificationToken = _userManager.GenerateUserToken(user, Purpose.EmailConfirm);

...mail send code...
}


public async Task VerifyEmailAsync(string userName, string verificationToken)
{
var user = await _userRepository.GetFirstOrDefault(u => u.UserName == userName) ?? throw new MilvaUserFriendlyException("InvalidVerificationToken");

// Validates token.
var result = _lazyUserManager.Value.VerifyUserToken(user, Purpose.EmailConfirm, verificationToken);

if (!result)
MilvaIdentityExceptionThrower.ThrowInvalidToken();// Throws MilvaUserFriendlyException with "IdentityInvalidToken" localizer key.

user.EmailConfirmed = true;

await _userRepository.UpdateAsync(user);
}
}

For more please see here.