Localizing/Overriding ASP.NET Identity 3 Error Messages in ASP.NET Core
Introduction:
If you ever develop an app with modern ASP.NET Identity framework then you know that at specific condition ASP.NET Identity returns some specific errors. For example, if email is invalid then framework will return Email 'x@y.com' is invalid message. These error messages are great but what if you want to override these error message. For example, if you are developing multi-lingual site then these error messages does not make any sense. ASP.NET Identity 3 makes it very easy to override/localize these identity error messages. There was no easy way to override these messages in ASP.NET Identity 2.x.x. In-fact 2 years ago, I opened an issue at codeplex. The issue was closed and then re-opened because the team now is planning to port this functionality form version 3 to version 2. In this post, I will show you how we can override these error messages in ASP.NET Identity 3.
Description:
Note that I am using beta5 at the time of writing. We just need add a class inheriting from IdentityErrorDescriber class,
001 |
public
class
MyIdentityErrorDescriber :
IdentityErrorDescriber
|
002 |
{
|
003 |
public
MyIdentityErrorDescriber()
|
004 |
{
|
005 |
}
|
006 |
007 |
public
override
IdentityError ConcurrencyFailure()
|
008 |
{
|
009 |
//return new IdentityError
|
010 |
//{
|
011 |
// Code =
nameof(ConcurrencyFailure),
|
012 |
// Description =
Resources.ConcurrencyFailure
|
013 |
//};
|
014 |
return
base.ConcurrencyFailure();
|
015 |
}
|
016 |
017 |
public
override
IdentityError DefaultError()
|
018 |
{
|
019 |
return
base.DefaultError();
|
020 |
}
|
021 |
022 |
public
override
IdentityError DuplicateEmail(string
email)
|
023 |
{
|
024 |
return
base.DuplicateEmail(email);
|
025 |
}
|
026 |
027 |
public
override
IdentityError DuplicateRoleName(string
name)
|
028 |
{
|
029 |
return
base.DuplicateRoleName(name);
|
030 |
}
|
031 |
032 |
public
override
IdentityError DuplicateUserName(string
name)
|
033 |
{
|
034 |
return
base.DuplicateUserName(name);
|
035 |
}
|
036 |
037 |
public
override
IdentityError InvalidEmail(string
email)
|
038 |
{
|
039 |
return
base.InvalidEmail(email);
|
040 |
}
|
041 |
042 |
public
override
IdentityError InvalidRoleName(string
name)
|
043 |
{
|
044 |
return
base.InvalidRoleName(name);
|
045 |
}
|
046 |
047 |
public
override
IdentityError InvalidToken()
|
048 |
{
|
049 |
return
base.InvalidToken();
|
050 |
}
|
051 |
052 |
public
override
IdentityError InvalidUserName(string
name)
|
053 |
{
|
054 |
return
base.InvalidUserName(name);
|
055 |
}
|
056 |
057 |
public
override
IdentityError LoginAlreadyAssociated()
|
058 |
{
|
059 |
return
base.LoginAlreadyAssociated();
|
060 |
}
|
061 |
062 |
public
override
IdentityError PasswordMismatch()
|
063 |
{
|
064 |
return
base.PasswordMismatch();
|
065 |
}
|
066 |
067 |
public
override
IdentityError PasswordRequiresDigit()
|
068 |
{
|
069 |
return
base.PasswordRequiresDigit();
|
070 |
}
|
071 |
072 |
public
override
IdentityError PasswordRequiresLower()
|
073 |
{
|
074 |
return
base.PasswordRequiresLower();
|
075 |
}
|
076 |
077 |
public
override
IdentityError
PasswordRequiresNonLetterAndDigit()
|
078 |
{
|
079 |
return
base.PasswordRequiresNonLetterAndDigit();
|
080 |
}
|
081 |
082 |
public
override
IdentityError PasswordRequiresUpper()
|
083 |
{
|
084 |
return
base.PasswordRequiresUpper();
|
085 |
}
|
086 |
087 |
public
override
IdentityError PasswordTooShort(int
length)
|
088 |
{
|
089 |
return
base.PasswordTooShort(length);
|
090 |
}
|
091 |
092 |
public
override
IdentityError UserAlreadyHasPassword()
|
093 |
{
|
094 |
return
base.UserAlreadyHasPassword();
|
095 |
}
|
096 |
097 |
public
override
IdentityError UserAlreadyInRole(string
role)
|
098 |
{
|
099 |
return
base.UserAlreadyInRole(role);
|
100 |
}
|
101 |
102 |
public
override
IdentityError UserLockoutNotEnabled()
|
103 |
{
|
104 |
return
base.UserLockoutNotEnabled();
|
105 |
}
|
106 |
107 |
public
override
IdentityError UserNotInRole(string
role)
|
108 |
{
|
109 |
return
base.UserNotInRole(role);
|
110 |
}
|
111 |
}
|
For each error, we just need to return IdentityError object which include Code and Description properties. Code is a short-error-message and Description include the details of the error. In real world application, we can get the error-messages from resource files. Currently, there is very little support of localization(including ASP.NET Core application cannot add resx file from Visual Studio). But its coming. Now we just need to register this class in Startup.ConfigureServices method,
1 |
services.AddTransient<IdentityErrorDescriber,
MyIdentityErrorDescriber>();
|
Summary:
ASP.NET Identity 3 makes it very easy to override the internal error messages that framework returns during validation. In this article, I showed you how we can localize these error messages.