gpt4 book ai didi

asp.net-mvc - ASP.NET MVC 4 SPA 如何删除登录页面的重定向?

转载 作者:行者123 更新时间:2023-12-02 09:18:53 25 4
gpt4 key购买 nike

我们正在与 SPA (MVC 4 + angularjs) 网站合作,我们希望拒绝未经授权的访问者访问静态文件。目前,对此静态文件的所有请求最终都会重定向到登录页面。我们希望收到 401 或 403 错误代码。该文件也可以通过 ajax 请求。

web.config:

    <authentication mode="None">
</authentication>

静态文件夹中的Web.config:

<configuration>
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />

<handlers>
<clear/>
<add type="System.Web.StaticFileHandler" path="*" verb="*" name="StaticFileHandler"/>
</handlers>
</system.webServer>
</configuration>

我想知道我们如何拒绝未经授权的访问者访问静态文件(不重定向到登录页面)?

最佳答案

我最终得到了这个解决方案:

  1. web.config添加到静态文件夹中。

    <configuration>
    <system.web>
    <authorization>
    <deny users="?"/>
    </authorization>
    </system.web>
    <system.webServer>
    <handlers>
    <clear/>
    <add type="System.Web.StaticFileHandler" path="*" verb="*" name="StaticFileHandler"/>
    </handlers>
    </system.webServer>
    </configuration>
  2. 添加特殊模块(在网站根目录 web.config 中),以抑制重定向到登录页面。

<modules runAllManagedModulesForAllRequests="false">   
<add
name="SuppressRedirect"
type="Web.Common.Modules.SuppressRedirectModule, Web.Common"/>
</modules>

以及下面的模块代码

using System;
using System.Web;

namespace Web.Common.Modules
{
public class SuppressRedirectModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.AuthenticateRequest += this.OnEndRequest;
}

private void OnEndRequest(object sender, EventArgs eventArgs)
{
HttpContext context = ((HttpApplication)sender).Context;
context.Response.SuppressFormsAuthenticationRedirect = true;
}

public void Dispose()
{
}
}
}

关于asp.net-mvc - ASP.NET MVC 4 SPA 如何删除登录页面的重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16067433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com