【发布时间】:2020-02-26 12:52:53
【问题描述】:
我试图将单个路由值从我的 Action 方法传递到 HTML。我正在尝试将保存在我的 url 中的单个令牌值传递给另一个 Action 方法。
我的操作方法
public async Task<IActionResult> LogIn(User user)
{
try
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(
Encoding.UTF8.GetBytes(user.Username + ":" + user.Password)));
HttpResponseMessage clientTask = await client.GetAsync("https://localhost:44324/api/Auth/LogIn");
if (clientTask.IsSuccessStatusCode)
{
string txtBlock = await clientTask.Content.ReadAsStringAsync();
var tokenObject = JsonConvert.DeserializeObject<SessionAPI>(txtBlock);
client.DefaultRequestHeaders.Authorization = null;
Request.RouteValues["token"] = tokenObject.Token;
return RedirectToAction("Index", "Home", Request.RouteValues);
}
else
return View("LogInIndex", user);
}
catch(Exception e)
{
throw new Exception("An Error has occured" + e);
}
}
我的 HTML
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
<p></p>
<p></p>
<div>
@using (Html.BeginForm("LogOut", "Auth", FormMethod.Get))
{
<input type="submit" value="Log Out" />
}
</div>
<div>
@using (Html.BeginForm("ListAll", "User", FormMethod.Get)) // <-- Trying to pass a route value for this form
{
<input type="submit" value="List All" />
}
</div>
<div>
@using (Html.BeginForm("CreateIndex", "User", FormMethod.Post))
{
<input type="submit" value="Create a User" />
}
</div>
</div>
基本思想是拥有一个存储在 URL 中的令牌,并在传递新请求时与它一起发送并分配给标头以进行身份验证
【问题讨论】:
标签: c# asp.net .net asp.net-core .net-core