ok, also versuche ich mich mit web api zu beschäftigen, und ich wollte eine mokup-login-seite erstellen, die username und passwort an einen authenticate controller sendet. Allerdings scheint ich nichts in meinem Beitrag zu bekommen
Das verursacht dann Probleme, wenn ich versuche, den Benutzernamen und das Passwort in meinem Controller zu validieren:
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]
[Route("api/login")]
public HttpResponseMessage Login([FromBody]LoginCredentials credentials)
{
Logout();
credentials.Username = credentials.Username ?? string.Empty;
credentials.Password = credentials.Password ?? string.Empty;
var authResult = Authentication.Authenticate(credentials.Username,
credentials.Password);
if (authResult == null || authResult.User == null)
return Authentication.GetUnauthorisedResponse();
if (!authResult.UseAllowed)
return Authentication.GetNotUseAllowedResponse
(authResult.Status);
var authenticationManager = Request.GetOwinContext().Authentication;
authenticationManager.SignIn(authResult.User.ToIdentity());
return new HttpResponseMessage(HttpStatusCode.NoContent);;
}
}
Meine Form sieht also so aus:
<form autocomplete="off" method="post" action="/api/login" name="myLogin">
<fieldset>
<legend>Login credentials</legend>
<div class="form-group">
<label for="Username">Username</label>
<input id="Username" type="text" />
</div>
<div>
<label for="Password">Password</label>
<input id="Password" type="password" />
<span class="help-block" data-validation="Password"></span>
</div>
<div class="form-group center">
<button type="submit" class="btn btn-primary" data-disable-on-submit="true">Log
on</button>
</div>
</fieldset>
</form>
nicht sicher, was ich habe, ist nicht korrekt.
Haben Sie versucht, den name
der Eingabeeigenschaft wie die Namen der Ansichtsmodelleigenschaften anzugeben?
<input id="Username" name="Username" type="text" />
Dadurch werden Ihre Formulardaten an das Ansichtsmodellobjekt gebunden.