using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Localization;
namespace gehGassi.Web.Helper
{
public class LocalizedIdentityErrorDescriber : IdentityErrorDescriber
{
///
/// The
/// used to localize the strings
///
private readonly IStringLocalizer localizer;
private const string PREFIX = "IdentityError_";
///
/// Initializes a new instance of the class.
///
///
/// The
/// that we will use to localize the strings
///
public LocalizedIdentityErrorDescriber(IStringLocalizer localizer)
{
this.localizer = localizer;
}
///
/// Returns the default .
///
/// The default
public override IdentityError DefaultError()
{
return this.GetErrorByCode(PREFIX + nameof(DefaultError));
}
///
/// Returns an indicating a concurrency failure.
///
/// An indicating a concurrency failure.
public override IdentityError ConcurrencyFailure()
{
return this.GetErrorByCode(PREFIX + nameof(ConcurrencyFailure));
}
///
/// Returns an indicating a password mismatch.
///
/// An indicating a password mismatch.
public override IdentityError PasswordMismatch()
{
return this.GetErrorByCode(PREFIX + nameof(PasswordMismatch));
}
///
/// Returns an indicating an invalid token.
///
/// An indicating an invalid token.
public override IdentityError InvalidToken()
{
return this.GetErrorByCode(PREFIX + nameof(InvalidToken));
}
///
/// Returns an indicating an external login is already associated with an account.
///
/// An indicating an external login is already associated with an account.
public override IdentityError LoginAlreadyAssociated()
{
return this.GetErrorByCode(PREFIX + nameof(LoginAlreadyAssociated));
}
///
/// Returns an indicating the specified user is invalid.
///
/// The user name that is invalid.
/// An indicating the specified user is invalid.
public override IdentityError InvalidUserName(string userName)
{
return this.FormatErrorByCode(PREFIX + nameof(InvalidUserName), (object)userName);
}
///
/// Returns an indicating the specified is invalid.
///
/// The email that is invalid.
/// An indicating the specified is invalid.
public override IdentityError InvalidEmail(string email)
{
return this.FormatErrorByCode(PREFIX + nameof(InvalidEmail), (object)email);
}
///
/// Returns an indicating the specified already exists.
///
/// The user name that already exists.
/// An indicating the specified already exists.
public override IdentityError DuplicateUserName(string userName)
{
return this.FormatErrorByCode(PREFIX + nameof(DuplicateUserName), (object)userName);
}
///
/// Returns an indicating the specified is already associated with an account.
///
/// The email that is already associated with an account.
/// An indicating the specified is already associated with an account.
public override IdentityError DuplicateEmail(string email)
{
return this.FormatErrorByCode(PREFIX + nameof(DuplicateEmail), (object)email);
}
///
/// Returns an indicating the specified name is invalid.
///
/// The invalid role.
/// An indicating the specific role name is invalid.
public override IdentityError InvalidRoleName(string role)
{
return this.FormatErrorByCode(PREFIX + nameof(InvalidRoleName), (object)role);
}
///
/// Returns an indicating the specified name already exists.
///
/// The duplicate role.
/// An indicating the specific role name already exists.
public override IdentityError DuplicateRoleName(string role)
{
return this.FormatErrorByCode(PREFIX + nameof(DuplicateRoleName), (object)role);
}
///
/// Returns an indicating a user already has a password.
///
/// An indicating a user already has a password.
public override IdentityError UserAlreadyHasPassword()
{
return this.GetErrorByCode(PREFIX + nameof(UserAlreadyHasPassword));
}
///
/// Returns an indicating user lockout is not enabled.
///
/// An indicating user lockout is not enabled..
public override IdentityError UserLockoutNotEnabled()
{
return this.GetErrorByCode(PREFIX + nameof(UserLockoutNotEnabled));
}
///
/// Returns an indicating a user is already in the specified .
///
/// The duplicate role.
/// An indicating a user is already in the specified .
public override IdentityError UserAlreadyInRole(string role)
{
return this.FormatErrorByCode(PREFIX + nameof(UserAlreadyInRole), (object)role);
}
///
/// Returns an indicating a user is not in the specified .
///
/// The duplicate role.
/// An indicating a user is not in the specified .
public override IdentityError UserNotInRole(string role)
{
return this.FormatErrorByCode(PREFIX + nameof(UserNotInRole), (object)role);
}
///
/// Returns an indicating a password of the specified does not meet the minimum length requirements.
///
/// The length that is not long enough.
/// An indicating a password of the specified does not meet the minimum length requirements.
public override IdentityError PasswordTooShort(int length)
{
return this.FormatErrorByCode(PREFIX + nameof(PasswordTooShort), (object)length);
}
///
/// Returns an indicating a password entered does not contain a non-alphanumeric character, which is required by the password policy.
///
/// An indicating a password entered does not contain a non-alphanumeric character.
public override IdentityError PasswordRequiresNonAlphanumeric()
{
return this.GetErrorByCode(PREFIX + nameof(PasswordRequiresNonAlphanumeric));
}
///
/// Returns an indicating a password entered does not contain a numeric character, which is required by the password policy.
///
/// An indicating a password entered does not contain a numeric character.
public override IdentityError PasswordRequiresDigit()
{
return this.GetErrorByCode(PREFIX + nameof(PasswordRequiresDigit));
}
///
/// Returns an indicating a password entered does not contain a lower case letter, which is required by the password policy.
///
/// An indicating a password entered does not contain a lower case letter.
public override IdentityError PasswordRequiresLower()
{
return this.GetErrorByCode(PREFIX + nameof(PasswordRequiresLower));
}
///
/// Returns an indicating a password entered does not contain an upper case letter, which is required by the password policy.
///
/// An indicating a password entered does not contain an upper case letter.
public override IdentityError PasswordRequiresUpper()
{
return this.GetErrorByCode(PREFIX + nameof(PasswordRequiresUpper));
}
public override IdentityError PasswordRequiresUniqueChars(int uniqueChars)
{
return this.FormatErrorByCode(PREFIX + nameof(PasswordRequiresUniqueChars), (object)uniqueChars);
}
public override IdentityError RecoveryCodeRedemptionFailed()
{
return this.GetErrorByCode(PREFIX + nameof(RecoveryCodeRedemptionFailed));
}
/// Returns a localized for the provided code.
/// The error's code.
/// A localized .
private IdentityError GetErrorByCode(string code)
{
return new IdentityError()
{
Code = code,
Description = this.localizer.GetString(code)
};
}
/// Formats a localized for the provided code.
/// The error's code.
/// The parameters to format the string with.
/// A localized .
private IdentityError FormatErrorByCode(string code, params object[] parameters)
{
return new IdentityError
{
Code = code,
Description = string.Format(this.localizer.GetString(code, parameters))
};
}
}
}