我已经HttpModule
实现了一个实施方式,该实施方式应该限制/courses/
对我的网站目录的访问,但是它存在一个主要问题。
Request.IsAuthenticated
一直都是false
。
这是代码:
using System;
using System.Web;
public class CourseAuthenticationModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(BeginRequest);
}
public void BeginRequest(Object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
HttpContext context = app.Context;
HttpRequest request = context.Request;
HttpResponse response = context.Response;
if (request.Path.ToLower().StartsWith("/courses/")
&& !request.IsAuthenticated)
{
response.Redirect("/");
}
}
}
我不知道为什么会发生这种情况,但是访问目录时条件总是会评估为这种情况。true
/courses/
编辑:
我在Web.Config中找到了这个。不确定是否相关。
<authentication mode="Forms">
<forms loginUrl="userlogin.asp" name=".cc" protection="All" path="/" timeout="2880" slidingExpiration="true" />
</authentication>
难道我做错了什么?我怎样才能解决这个问题?
将BeginRequest作为第一个事件还为时过早,无法询问用户是否已通过身份验证。
在这一点上,您可以简单地检查其是否通过直接读取cookie进行身份验证,如下所示:
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];
if (null == authCookie || FormsAuthentication.Decrypt(authCookie.Value) == null)
{
// is not authenticated
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句