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, 

 

    public class MyIdentityErrorDescriber : IdentityErrorDescriber
    {
        public MyIdentityErrorDescriber()
        {
        }

        public override IdentityError ConcurrencyFailure()
        {
            //return new IdentityError
            //{
            //    Code = nameof(ConcurrencyFailure),
            //    Description = Resources.ConcurrencyFailure
            //};
            return base.ConcurrencyFailure();
        }

        public override IdentityError DefaultError()
        {
            return base.DefaultError();
        }

        public override IdentityError DuplicateEmail(string email)
        {
            return base.DuplicateEmail(email);
        }

        public override IdentityError DuplicateRoleName(string name)
        {
            return base.DuplicateRoleName(name);
        }

        public override IdentityError DuplicateUserName(string name)
        {
            return base.DuplicateUserName(name);
        }

        public override IdentityError InvalidEmail(string email)
        {
            return base.InvalidEmail(email);
        }

        public override IdentityError InvalidRoleName(string name)
        {
            return base.InvalidRoleName(name);
        }

        public override IdentityError InvalidToken()
        {
            return base.InvalidToken();
        }

        public override IdentityError InvalidUserName(string name)
        {
            return base.InvalidUserName(name);
        }

        public override IdentityError LoginAlreadyAssociated()
        {
            return base.LoginAlreadyAssociated();
        }

        public override IdentityError PasswordMismatch()
        {
            return base.PasswordMismatch();
        }

        public override IdentityError PasswordRequiresDigit()
        {
            return base.PasswordRequiresDigit();
        }

        public override IdentityError PasswordRequiresLower()
        {
            return base.PasswordRequiresLower();
        }

        public override IdentityError PasswordRequiresNonLetterAndDigit()
        {
            return base.PasswordRequiresNonLetterAndDigit();
        }

        public override IdentityError PasswordRequiresUpper()
        {
            return base.PasswordRequiresUpper();
        }

        public override IdentityError PasswordTooShort(int length)
        {
            return base.PasswordTooShort(length);
        }

        public override IdentityError UserAlreadyHasPassword()
        {
            return base.UserAlreadyHasPassword();
        }

        public override IdentityError UserAlreadyInRole(string role)
        {
            return base.UserAlreadyInRole(role);
        }

        public override IdentityError UserLockoutNotEnabled()
        {
            return base.UserLockoutNotEnabled();
        }

        public override IdentityError UserNotInRole(string role)
        {
            return base.UserNotInRole(role);
        }
    }

 

                    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,

 

       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.

No Comments