【问题标题】:Using OWIN to authorize classic ASP pages使用 OWIN 授权经典 ASP 页面
【发布时间】:2016-04-28 09:48:44
【问题描述】:

我有一个古老的 Classic ASP 网站,它曾经使用 Windows 身份验证进行访问控制,但这已不再适用。我想我可能会尝试将这个网站包装在一个 ASP.NET MVC 5 应用程序中,所以我:

  • 使用 ASP.NET Identity 创建了一个新网站
  • 创建了一个用户
  • 将 Classic ASP 网站复制到根目录中
  • 浏览了经典的 ASP 网站

现在我需要做的是要求对所有 .asp 页面进行授权,以便只有授权用户才能看到它们。这样做的最佳方法是什么?也许我可以用 OWIN 做点什么?

Crispin

【问题讨论】:

    标签: asp-classic asp.net-identity httpmodule


    【解决方案1】:

    this example 之后,我创建了一个如下所示的 HttpModule:

    public class ClassicAspAuthorization : IHttpModule
        {
        private MyEventHandler _eventHandler = null;
    
        public void Init(HttpApplication context)
            {
            context.BeginRequest += new EventHandler(OnBeginRequest);
            }
    
        public delegate void MyEventHandler(Object s, EventArgs e);
    
        public event MyEventHandler MyEvent
            {
            add { _eventHandler += value; }
            remove { _eventHandler -= value; }
            }
    
        public void OnBeginRequest(Object s, EventArgs e)
            {
            HttpApplication app = s as HttpApplication;
    
            if (app.Request.CurrentExecutionFilePathExtension.EndsWith(".asp") == true && blnIsAuthenticated() == false)
                {
                app.Context.Response.Redirect("/Account/Login");
                }
    
            if (_eventHandler != null)
                {
                _eventHandler(this, null);
                }
            }
    

    确定用户是否通过身份验证的布尔 (blnIsAuthenticated) 方法源自我删除了行的Stackoverflow answer

    var identity = new ClaimsIdentity(claims, authenticationType, ClaimTypes.Name, ClaimTypes.Role);
    
    var principal = new ClaimsPrincipal(identity);
    
    System.Threading.Thread.CurrentPrincipal = principal;
    HttpContext.Current.User = principal;
    

    并将其替换为我自己的声明检查以确定用户是否已通过身份验证。返回了一个适当的布尔值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-16
      • 2022-07-01
      • 1970-01-01
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多