Asp.net mvc 通过 httpClient 连接到 webApi
Asp.net mvc connect to webApi by httpClient
我有 mvc 应用程序和 webApi 应用程序。我需要通过 webApi 登录 asp.net mvc。当我使用这种方法时:
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;
}
}
我的 mvc 没有从视图中绑定模型。
动作方法
[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");
}
是模特
public class LoginModelView
{
public string Login { get; set; }
public string Password { get; set; }
}
这是我的观点
@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>
}
当我不使用那个方法时,一切正常。
相同的代码在另一个应用程序中工作。
请有人可以帮助我..
我认为您是从数据发布到获取方法 (GetToken)。
我注意到 LoginModelView
class 中有 Login
属性 并且您在 controller.I 中创建相同的对象名称 LoginModelView login
更改该代码及其对我的工作。
型号:-
[HttpGet]
public ActionResult SignIn()
{
var loginModelView = new LoginModelView();
return View("SignIn", loginModelView);
}
[HttpPost]
public ActionResult SignIn(LoginModelView loginModelView)
{
string token = DirService.GetToken(loginModelView);
Session["token"] = token;
return RedirectToAction("success");
}
希望它的工作!!!
编码愉快!!
我有 mvc 应用程序和 webApi 应用程序。我需要通过 webApi 登录 asp.net mvc。当我使用这种方法时:
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;
}
}
我的 mvc 没有从视图中绑定模型。 动作方法
[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");
}
是模特
public class LoginModelView
{
public string Login { get; set; }
public string Password { get; set; }
}
这是我的观点
@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>
}
当我不使用那个方法时,一切正常。 相同的代码在另一个应用程序中工作。 请有人可以帮助我..
我认为您是从数据发布到获取方法 (GetToken)。
我注意到 LoginModelView
class 中有 Login
属性 并且您在 controller.I 中创建相同的对象名称 LoginModelView login
更改该代码及其对我的工作。
型号:-
[HttpGet]
public ActionResult SignIn()
{
var loginModelView = new LoginModelView();
return View("SignIn", loginModelView);
}
[HttpPost]
public ActionResult SignIn(LoginModelView loginModelView)
{
string token = DirService.GetToken(loginModelView);
Session["token"] = token;
return RedirectToAction("success");
}
希望它的工作!!!
编码愉快!!