I have mvc application and webApi application. I need sign in in asp.net mvc through webApi. When i use this method:
public static string GetToken(LoginModelView login)
{
var pairs = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>( "grant_type", "password" ),
new KeyValuePair<string, string>( "username", login.Login ),
new KeyValuePair<string, string> ( "Password", login.Password )
};
var content = new FormUrlEncodedContent(pairs);
using (var client = new HttpClient())
{
var response =
client.PostAsync(URL + "/Token", content).Result;
var responseJson = response.Content.ReadAsStringAsync().Result;
var dictionaryToken = JsonConvert.DeserializeObject<Dictionary<string,string>>(responseJson);
string token = dictionaryToken["access_token"];
return token;
}
}
My mvc don't bind model from view. Action methods
[HttpGet]
public ActionResult SignIn()
{
var login = new LoginModelView();
return View("SignIn",login);
}
[HttpPost]
public ActionResult SignIn(LoginModelView login)
{
string token = DirService.GetToken(login);
Session["token"] = token;
return RedirectToAction("success");
}
It's model
public class LoginModelView
{
public string Login { get; set; }
public string Password { get; set; }
}
It's my view
@model AdminTool.Models.LoginModelView
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>LoginModelView</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Login, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Login, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Login, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Login" class="btn btn-default" />
</div>
</div>
</div>
}
when i don't use that method, everything is ok. The same code works in another application. Somebody can help me, please..
don't bind model. Is the action being called/hit?