After using the online tutorial on using token-based authentication using OWIN, I was able to run a test application with a hard coded username / password, as the demo did.
However, now I want to use my model from my web application.
My authentication happens, as the demo says, in this piece of code.
namespace UI { public class AuthorisationServerProvider : OAuthAuthorizationServerProvider { public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) { context.Validated(); // Means I have validated the client. } public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { // Here we validate the user... var identity = new ClaimsIdentity(context.Options.AuthenticationType); if (context.UserName == "user" && context.Password == "password") { identity.AddClaim(new Claim(ClaimTypes.Role, "admin")); identity.AddClaim(new Claim("username", "user")); identity.AddClaim(new Claim(ClaimTypes.Name, "My Full Name")); context.Validated(identity); } else { context.SetError("Invalid grant", "Username or password are incorrect"); return; } } } }
I have a WebAPI controller from which I get the model, and ... not sure how to call the above code from my webapi controller. At the moment, the above code is expecting a call to myurl / token, which was defined in the start code.
public class Startup { public void Configuration(IAppBuilder app) {
So, I assume that the url of my webapi call should be / token? So in my (knockout model) in my user interface I tried this:
Login() { var data = { username : this.login.emailAddress(), password : this.login.password(), RememberMe: this.login.rememberMe(), grant_type: "password" } return $.ajax({ type: "POST", data: data ? JSON.stringify(data) : null, dataType: "json", url: "/token", contentType: "application/json" }).done((reply) => { alert("Done!"); }); }
But I get an exception:
"error": "unsupported_grant_type"
In Postman, I can authenticate a hard-coded username / password.

But I'm not sure how to connect my api call from my user interface for authentication.
I was hoping to create a "Login" method on my api controller (ASP.Net WebAPI), for example:
[Route("login"), HttpPost, AllowAnonymous] public ReplyDto Login(LoginRequest login) { ReplyDto reply = _userService.Login(login.Email, login.Password); return reply; }
So, my _userService checks if the user is in the database ... if so, call my OAuth authentication here by passing a few parameters. But not sure if this is possible. Can I call my authentication from this api method? However, I will need to remove the bit / token.