【发布时间】:2012-07-22 13:31:53
【问题描述】:
我们有一个旧的经典 ASP 应用程序,用于管理和启动我们的其他 Web 应用程序。
它启动应用的方式如下:
<form name="frmMain" action="http://xxxx/mvc3app/Index" target=_self method=post>
<script language="javascript">
frmMain.submit();
</script>
用户登录名和密码作为请求的一部分传递。
为了在 ASP.NET 应用程序中对用户进行身份验证,我调用了以下 AuthenticateUser 函数:
public bool AuthenticateUser()
{
var userName = Context.Request["txtName"];
var password = Context.Request["txtPassword"];
if (Membership.ValidateUser(userName, password))
{
FormsAuthentication.SetAuthCookie(userName, true);
}
}
我假设调用 AuthenticateUser 的正确位置是 global.asax 中的 Session_Start() 方法,但提交“frmMain”时似乎没有调用此方法。它似乎间歇性地工作 - 如果我完全关闭 IE,请再试一次,然后手动输入 URL。
void Session_Start(object sender, EventArgs e)
{
Log("In Session Start");
AthenticateUser();
}
在我的 ASP.NET 应用中对用户进行身份验证的正确位置是什么?
这里是来自开发工具的屏幕验证失败 - Session_Start() 没有被调用。
编辑
看起来这不起作用,因为 IsAuthenticated 属性仅在后续请求中设置,这导致索引操作上的身份验证失败。
我现在将对此进行测试,但请参阅 Who sets the IsAuthenticated property of the HttpContext.User.Identity
解决方案:
调用 SetAuthCookie 后第一个错误未重定向,这导致索引视图验证失败。
我也意识到没有必要将它放在 global.asax 中,但我宁愿重定向到 LogOn 操作,而不是直接转到 index 操作:
public ActionResult LogOn()
{
var userName = Context.Request["txtName"];
var password = Context.Request["txtPassword"];
if (Membership.ValidateUser(userName, password))
{
FormsAuthentication.SetAuthCookie(userName, false);
return RedirectToAction("Index", "Index");
}
else
{
return RedirectToAction("IncorrectLogin", "Index");
}
}
【问题讨论】:
-
在您的登录操作中......
-
Session_Start在用户第一次访问您的网页时被调用。这是一个新会话的开始。考虑查看 Webform 应用程序的页面和应用程序生命周期。 =) -
在我看来这不像是经典的 ASP。您是说 Asp.net 网络表单吗?
-
@podiluska 不,重定向的页面是经典的asp(doapp.asp)
标签: c# asp.net asp.net-mvc-3 authentication global-asax