我想讓FormsAuthentication與我的Razor應用程序(MVC 3)一起工作。我有一個LoginController調用我的LoginPage(它在Views/Shared中);我的web.config將LoginUrl設置爲「/ Login /」。當應用程序嘗試啓動主頁面時,[Authorize]行會正確顯示LoginPage,但這是問題出現的位置。FormsAuthentication與剃刀不工作
這裏是我的LoginController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ABMCEditAndReports.Controllers
{
public class LoginController : Controller
{
//
// GET: /Login/
public ActionResult Index()
{
return View("LoginPage");
}
}
}
這裏是我的LoginPage.cshtml:
@{
ViewBag.Title = "Login Page";
ViewBag.Header = "Login Page";
}
<script type="text/javascript">
$.ajaxSetup({ cache: false });
function onClickLogin() {
if (Membership.ValidateUser(txtUsername.Text, txtPassword.Text)) {
FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, false);
}
else {
lblErrorMessage.Text = "This works better if you use YOUR user name and password";
}
}
</script>
<div style="text-align:center;">
<h2>Login Required</h2>
User Name:
<input type="text" name="txtUsername" />
<br />
Password:
<input type="password" name="txtPassword" />
<br />
<input type="button" id="btnLogin" value="Login" onclick="onClickLogin()" />
<br />
<label id="lblErrorMessage" style="color:Red"></label>
</div>
第一個問題是,當我啓動應用程序,VS停在與$ .ajaxSetup線「Microsoft JScript運行時錯誤:'$'未定義」。
當我發表評論時,頁面顯示出來,但是當我按下「登錄」按鈕時,它停在Membership.ValidateUser行並顯示「Microsoft JScript運行時錯誤:'成員資格'未定義」。
請注意,$ .ajaxSetup行對我所有其他視圖頁都正常工作。
我已經嘗試在LoginPage.cshtml中添加「@using System.Web.Security」,並向web.config(主視圖和主視圖中的一個)添加命名空間System.Web.Security;沒有任何修復它。
我對此有何看法?我應該在.cs文件中調用ValidateUser嗎?如果是這樣,我該如何調用RedirectFromLoginPage? (爲什麼不接受$ .ajaxSetup?)
不能混合服務器端代碼和客戶端代碼一樣, ,如果不包含它,你就不能使用jQuery。 – SLaks