FormsAuthentication剃刀不工作 [英] FormsAuthentication with Razor not working
问题描述
我试图让FormsAuthentication与我的剃刀的应用程序(MVC 3)工作。我有叫我LoginPage一个LoginController中(这是在查看/共享);我的web.config已LoginUrl设置为/登录/。当应用程序试图打开主页时,[授权]行带来了LoginPage正确的,但是这也正是问题的开始。
I am trying to get FormsAuthentication to work with my Razor app (MVC 3). I have a LoginController that calls my LoginPage (which is in Views/Shared); my web.config has LoginUrl set to "/Login/". When the app tries to bring up the main page, the [Authorize] line brings up LoginPage correctly, but that's where the problems start.
下面是我的LoginController.cs:
Here's 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
{
//
// GET: /Login/
public ActionResult Index()
{
return View("LoginPage");
}
}
}
下面是我的LoginPage.cshtml:
Here's 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>
第一个问题是,当我启动应用程序,VS停在有Microsoft JScript运行时错误:$未定义的$ .ajaxSetup行
The first problem is, when I start the app, VS stops at the $.ajaxSetup line with "Microsoft JScript runtime error: '$' is undefined".
当我评论说出来,页面显示出来,但是当我preSS的登录按钮,它停在有Microsoft JScript运行错误:'会员'是未定义的Membership.ValidateUser行。
When I comment that out, the page shows up, but when I press the "Login" button, it stops at the Membership.ValidateUser line with "Microsoft JScript runtime error: 'Membership' is undefined".
注意$ .ajaxSetup线正常工作在我所有的其他视图页面。
Note that the $.ajaxSetup line works fine on all of my other view pages.
我曾尝试加入@using System.Web.Security来LoginPage.cshtml,并添加命名空间System.Web.Security到web.config文件(包括主之一,一个在/浏览次数);没有修复它。
I have tried adding "@using System.Web.Security" to LoginPage.cshtml, and adding namespace System.Web.Security to web.config (both the main one and the one in /Views); nothing fixes it.
我要对这个正确的方式?我应该打电话的ValidateUser在cs文件?如果是这样,我怎么叫RedirectFromLoginPage? (为什么是不是接受$ .ajaxSetup?)
Am I going about this the right way? Should I be calling ValidateUser in a .cs file? If so, how do I call RedirectFromLoginPage? (And why isn't it accepting $.ajaxSetup?)
推荐答案
FormsAuthentication
确实与ASP.NET MVC的工作,但尝试验证上的服务器。这里有一个例子:
FormsAuthentication
does work with ASP.NET MVC, but try authenticating on the server. Here's an example:
控制器:
namespace ABMCEditAndReports.Controllers
{
public class LoginController : Controller
{
//
// GET: /Login/
public ActionResult Index(string returnUrl = null)
{
this.ViewBag.ReturnUrl = returnUrl;
return View("LoginPage");
}
public ActionResult Index(LogInViewModel model, string returnUrl)
{
if (this.ModelState.IsValid && Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, false);
return this.Redirect(returnUrl);
}
this.ModelState.AddModelError("", "The user name or password provided is incorrect.");
return this.View(model);
}
}
}
型号:
namespace ABMCEditAndReports.Models
{
public class LogInViewModel
{
[Display(Name = "User Name")]
public string UserName { get; set; }
[Display(Name = "Password")]
[DataType(DataType.Password)]
public string Password { get; set; }
}
}
查看:
@model ABMCEditAndReports.Models.LogInViewModel
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }))
{
@Html.ValidationSummary()
<h2>Please sign in</h2>
<div>
@Html.DisplayNameFor(model => model.UserName)
@Html.EditorFor(model => model.UserName)
</div>
<div>
@Html.DisplayNameFor(model => model.Password)
@Html.EditorFor(model => model.Password)
</div>
<button type="submit">Sign in</button>
}
这篇关于FormsAuthentication剃刀不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!