Skip to content

Instantly share code, notes, and snippets.

@buehler
Created October 28, 2019 11:29
Show Gist options
  • Save buehler/f168b4ff0f9c82e8eee407fa19f470de to your computer and use it in GitHub Desktop.
Save buehler/f168b4ff0f9c82e8eee407fa19f470de to your computer and use it in GitHub Desktop.
Identity Error Describer (asp.net core identity) with string localizer.
public class LocalizedIdentityErrors : IdentityErrorDescriber
{
private readonly IStringLocalizer<LocalizedIdentityErrors> _localizer;
public LocalizedIdentityErrors(IStringLocalizer<LocalizedIdentityErrors> localizer)
{
_localizer = localizer;
}
public override IdentityError DefaultError() => new IdentityError
{
Code = nameof(DefaultError),
Description = _localizer[nameof(DefaultError)],
};
public override IdentityError ConcurrencyFailure() =>
new IdentityError
{
Code = nameof(ConcurrencyFailure),
Description = _localizer[nameof(ConcurrencyFailure)],
};
public override IdentityError PasswordMismatch() => new IdentityError
{
Code = nameof(PasswordMismatch),
Description = _localizer[nameof(PasswordMismatch)],
};
public override IdentityError InvalidToken() => new IdentityError
{
Code = nameof(InvalidToken),
Description = _localizer[nameof(InvalidToken)],
};
public override IdentityError LoginAlreadyAssociated() => new IdentityError
{
Code = nameof(LoginAlreadyAssociated),
Description = _localizer[nameof(LoginAlreadyAssociated)],
};
public override IdentityError InvalidUserName(string userName) => new IdentityError
{
Code = nameof(InvalidUserName),
Description = _localizer.GetString(nameof(InvalidUserName), userName),
};
public override IdentityError InvalidEmail(string email) => new IdentityError
{
Code = nameof(InvalidEmail),
Description = _localizer.GetString(nameof(InvalidEmail), email),
};
public override IdentityError DuplicateUserName(string userName) => new IdentityError
{
Code = nameof(DuplicateUserName),
Description = _localizer.GetString(nameof(DuplicateUserName), userName),
};
public override IdentityError DuplicateEmail(string email) => new IdentityError
{
Code = nameof(DuplicateEmail),
Description = _localizer.GetString(nameof(DuplicateEmail), email),
};
public override IdentityError InvalidRoleName(string role) => new IdentityError
{
Code = nameof(InvalidRoleName),
Description = _localizer.GetString(nameof(InvalidRoleName), role),
};
public override IdentityError DuplicateRoleName(string role) => new IdentityError
{
Code = nameof(DuplicateRoleName),
Description = _localizer.GetString(nameof(DuplicateRoleName), role),
};
public override IdentityError UserAlreadyHasPassword() => new IdentityError
{
Code = nameof(UserAlreadyHasPassword),
Description = _localizer[nameof(UserAlreadyHasPassword)],
};
public override IdentityError UserLockoutNotEnabled() => new IdentityError
{
Code = nameof(UserLockoutNotEnabled),
Description = _localizer[nameof(UserLockoutNotEnabled)],
};
public override IdentityError UserAlreadyInRole(string role) => new IdentityError
{
Code = nameof(UserAlreadyInRole),
Description = _localizer.GetString(nameof(UserAlreadyInRole), role),
};
public override IdentityError UserNotInRole(string role) => new IdentityError
{
Code = nameof(UserNotInRole),
Description = _localizer.GetString(nameof(UserNotInRole), role),
};
public override IdentityError PasswordTooShort(int length) => new IdentityError
{
Code = nameof(PasswordTooShort),
Description = _localizer.GetString(nameof(PasswordTooShort), length),
};
public override IdentityError PasswordRequiresNonAlphanumeric() => new IdentityError
{
Code = nameof(PasswordRequiresNonAlphanumeric),
Description = _localizer[nameof(PasswordRequiresNonAlphanumeric)]
};
public override IdentityError PasswordRequiresDigit() => new IdentityError
{
Code = nameof(PasswordRequiresDigit),
Description = _localizer[nameof(PasswordRequiresDigit)],
};
public override IdentityError PasswordRequiresLower() => new IdentityError
{
Code = nameof(PasswordRequiresLower),
Description = _localizer[nameof(PasswordRequiresLower)],
};
public override IdentityError PasswordRequiresUpper() => new IdentityError
{
Code = nameof(PasswordRequiresUpper),
Description = _localizer[nameof(PasswordRequiresUpper)],
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment