サインインフォームがあり、
@if (@User.Identity.IsAuthenticated)
{
<div>Welcome @User.Identity.Name</div>
}
else
{
<input type="text" id="username" name="username" placeholder="" class="input-xlarge">
<input type="password" id="password" name="password" placeholder="" class="input-xlarge">
<button id="loginBtn" class="btn btn-success">Login</button>
}
一部のJQuery、
$("#loginBtn").click(function () {
Authenticate();
});
function Authenticate() {
$.post("/Home/Authenticate", { username: "John", password: "test" }, function (data) {
$('#loginArea').replaceWith(data);
});
}
そして、リクエストを処理するためのいくつかのサーバー側コード。
[HttpPost]
public ActionResult Authenticate(string username, string password)
{
var ticket = new FormsAuthenticationTicket(
1,
username,
DateTime.Now,
DateTime.Now.AddDays(5),
true,
string.Empty,
FormsAuthentication.FormsCookiePath);
var encTicket = FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
return PartialView("_login");
}
奇妙なことに、これは機能しますが、最初の呼び出しでは機能しません。ユーザーが認証されても、返されたビューは更新されません。ページを更新すると、すべてが期待どおりに機能します。問題は、これは単一ページのアプリであり、ページを強制的に更新したくないということです..
返された PartialView が「ようこそ」テキストをレンダリングしない理由はありますか?