public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
      IdentityUser user;

      var data = await context.Request.ReadFormAsync();

      var allowedOrigin = context.OwinContext.Get<string>("as:clientAllowedOrigin");

      if (allowedOrigin == null) allowedOrigin = "*";

      context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });

      using (AuthRepository _repo = new AuthRepository())
      {
        user = await _repo.FindUser(data["email"], context.Password);

        if (user == null)
        {
          context.SetError("invalid_grant", "The user email or password is incorrect.");
          return;
        }
      }

      var identity = new ClaimsIdentity(context.Options.AuthenticationType);
      identity.AddClaim(new Claim(ClaimTypes.Email, data["email"]));
      identity.AddClaim(new Claim(ClaimTypes.Role, "user"));
      identity.AddClaim(new Claim("sub", context.UserName));

      var props = new AuthenticationProperties(new Dictionary<string, string>
      {
          { 
              "as:client_id", (context.ClientId == null) ? string.Empty : context.ClientId
          },
          { 
              "userName", user.UserName
          },
          {
            "email", user.Email
          },
          {
            "id", user.Id
          }
      });

      var ticket = new AuthenticationTicket(identity, props);
      context.Validated(ticket);
    }