I'm posting user credentials from a web app to the web api which implements a provider that authenticates the user and responds with a valid token.
This is the method that posts:
public TokenModel RequestAPIToken(string username, string password)
{
var postData = new Dictionary<string, string>();
postData.Add("grant_type", "password");
postData.Add("username ", username);
postData.Add("password ", password);
HttpContent content = new FormUrlEncodedContent(postData);
_response = _client.PostAsync("token", content).Result;
var result = _response.Content.ReadAsAsync<TokenModel>().Result;
return result;
}
This is taken from the web api project:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
var _userServices = new UserServices();
User user = _userServices.GetValidatedUser(context.UserName, context.Password).FirstOrDefault();
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("userId", user.UserId.ToString()));
identity.AddClaim(new Claim("username", user.Username.ToString()));
context.Validated(identity);
}
The problem is that context.UserName and context.Password are always null! I have tried using key value pairs instead of a dictinary and I am using _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
Any advice please?