覆盖ASP.NET单个页面表单验证 [英] Override ASP.NET forms authentication for a single page

查看:78
本文介绍了覆盖ASP.NET单个页面表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的ASP.NET MVC应用程序,我们会自动将用户重定向到一个登录通过&LT页;认证> 部分<的System.Web> ,当他们试图访问授权,仅页面。问题是,在应用中,设计成由工具一起使用的中间的一个动作,需要返回上坏访问的直向上的HTTP 401响应。我怎样才能回到真实的HTTP 401 code的没有的重定向这个具体行动?

In our ASP.NET MVC application, we automatically redirect users to a log-on page via the <authentication> section of <system.web> when they attempt to access an authorized-only page. The problem is that one action in the middle of the application, designed to be used by a tool, needs to return a straight-up HTTP 401 response on bad access. How can I return a real HTTP 401 code without the redirect for this specific action?

推荐答案

下面的解决方案的工作,虽然我不能肯定它是最理想的:

The following solution works, although I'm not at all sure it's optimal:

public class HttpAuthenticationRequiredResult : ActionResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.HttpContext.Response;
        response.StatusCode = 401;
        response.AddHeader("WWW-Authenticate", "Basic realm=\"whatever\"");
        response.Flush();
        response.Close();
    }
}

可以然后返回上述结果,而不是一个HttpUnauthorizedResult的产生所需的401 code。这种感觉很klugy给我,但是。

You can then return the above result instead of an HttpUnauthorizedResult to generate the required 401 code. This feels quite klugy to me, however.

这篇关于覆盖ASP.NET单个页面表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆