Q:

How can I make a method in ASP.NET identity account controller return my own error message?

The controller has this function:
private IHttpActionResult GetErrorResult(IdentityResult result)
        {
            if (result == null)
            {
                return InternalServerError();
            }
            if (!result.Succeeded)
            {
                if (result.Errors != null)
                {
                    foreach (string error in result.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }
                if (ModelState.IsValid)
                {
                    // No ModelState errors are available to send, 
                    // so just return an empty BadRequest.
                    return BadRequest();
                }
                return BadRequest(ModelState);
            }
            return Ok();
        }

I have a forgot password method and I would like this to return an error if the username is not found:
public async Task<IHttpActionResult> ForgotPassword(ForgotPasswordBindingModel model)
        {
            var user = await UserManager.FindByNameAsync(model.UserName);
            if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
            {
                // I want to return a BadRequest with message "Username not found"
                // What I think is needed is to put that message into result.Errors 
                // but I am not sure how to do that
                return GetErrorResult(result);
            }

A:

You can create new IdentityResult object and pass error message to ctor. ( It specifies that result is failure and adds error message to Errors property). 
public async Task<IHttpActionResult> ForgotPassword(ForgotPasswordBindingModel model)
{
    var user = await UserManager.FindByNameAsync(model.UserName);
    if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
    {
        IdentityResult result = new IdentityResult("Username not found");
        return GetErrorResult(result);
    }
}

