html form post to web api 2controller c# dapper owin


ok, so i'm trying to get my head around web api, and i wanted to create a mokup login page, that posts username and password to a authenticate controller. However, i dont seem to be getting anything in my post

Post null

this is then causing issues, when I try and validate the usernaem and password in my controller:

public class LoginCredentials
    public string Username { get; set; }
    public string Password { get; set; }

public class AuthenticationController : ApiController
    /// <summary>
    /// Creates an authenticated session. If successfully authenticated returns            a 'Set-Cookie' header
    /// containing the cookie for future requests. On failed login attempt an HTTP 
                401 error is returned.
    /// </summary>
    [HttpPost, AllowAnonymous]
    public HttpResponseMessage Login([FromBody]LoginCredentials credentials)
        credentials.Username = credentials.Username ?? string.Empty;
        credentials.Password = credentials.Password ?? string.Empty;

        var authResult = Authentication.Authenticate(credentials.Username, 
        if (authResult == null || authResult.User == null)
            return Authentication.GetUnauthorisedResponse();
        if (!authResult.UseAllowed)
            return Authentication.GetNotUseAllowedResponse

        var authenticationManager = Request.GetOwinContext().Authentication;

        return new HttpResponseMessage(HttpStatusCode.NoContent);;

My form thus looks like:

  <form autocomplete="off" method="post" action="/api/login" name="myLogin">

        <legend>Login credentials</legend>

        <div class="form-group">
            <label for="Username">Username</label>
            <input id="Username" type="text" />

            <label for="Password">Password</label>
            <input id="Password" type="password" />
            <span class="help-block" data-validation="Password"></span>

        <div class="form-group center">
            <button type="submit" class="btn btn-primary" data-disable-on-submit="true">Log 

not sure what i've that isnt correct.

Accepted Answer

Did you try giving the inputs name property value same as the view model property names ?

 <input id="Username" name="Username" type="text" />

This will bind your form posted data to the view model object.

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why