加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – Request.IsAuthenticated永远不会成立

发布时间:2020-12-16 03:29:30 所属栏目:asp.Net 来源:网络整理
导读:我已经实现了一个HttpModule,它应该限制对我网站的/ courses /目录的访问,但是它有一个主要问题. Request.IsAuthenticated总是错误的. 这是代码: using System;using System.Web;public class CourseAuthenticationModule : IHttpModule{ public void Dispo
我已经实现了一个HttpModule,它应该限制对我网站的/ courses /目录的访问,但是它有一个主要问题.

Request.IsAuthenticated总是错误的.

这是代码:

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("/");
        }
    }
}

我不知道为什么会发生这种情况,但在访问/ courses /目录时,条件总是会评估为true.

编辑:

我在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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读