I am trying to get FormsAuthentication to work with my Razor application (MVC 3). I have a LoginController that calls my LoginPage (which is in Views / Shared); my web.config has the LoginUrl parameter set to "/ Login /". When an application tries to open the main page, the [Authorize] line will correctly return LoginPage, but where problems arise.
Here is my 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
{
public ActionResult Index()
{
return View("LoginPage");
}
}
}
Here is my 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>
The first problem is that when the application starts, VS stops at the $ .ajaxSetup line with "Microsoft JScript runtime error:" $ "is undefined".
, , "", Membership.ValidateUser " Microsoft JScript:" "- undefined".
, $.ajaxSetup .
"@using System.Web.Security" LoginPage.cshtml System.Web.Security web.config( , /); .
? ValidateUser .cs? , RedirectFromLoginPage? ( $.ajaxSetup?)