简体   繁体   English

绕过表单身份验证自动重定向到登录,如何?

[英]Bypass Forms Authentication auto redirect to login, How to?

I'm writing an app using asp.net-mvc deploying to iis6. 我正在使用asp.net-mvc部署到iis6编写应用程序。 I'm using forms authentication. 我正在使用表单身份验证。 Usually when a user tries to access a resource without proper authorization I want them to be redirected to a login page. 通常当用户尝试在没有适当授权的情况下访问资源时,我希望将它们重定向到登录页面。 FormsAuth does this for me easy enough. FormsAuth让我很容易做到这一点。

Problem: Now I have an action being accessed by a console app. 问题:现在我有一个控制台应用程序访问的操作。 Whats the quickest way to have this action respond w/ status 401 instead of redirecting the request to the login page? 什么是让这个动作响应w /状态401而不是将请求重定向到登录页面的最快方法?

I want the console app to be able to react to this 401 StatusCode instead of it being transparent. 我希望控制台应用程序能够对此401 StatusCode做出反应,而不是透明。 I'd also like to keep the default, redirect unauthorized requests to login page behavior. 我还想保留默认值,将未经授权的请求重定向到登录页面行为。

Note: As a test I added this to my global.asax and it didn't bypass forms auth: 注意:作为测试我将其添加到我的global.asax并且它没有绕过表单身份验证:

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
    HttpContext.Current.SkipAuthorization = true;
}

@Dale and Andy @Dale和Andy

I'm using the AuthorizeAttributeFilter provided in MVC preview 4. This is returning an HttpUnauthorizedResult. 我正在使用MVC预览4中提供的AuthorizeAttributeFilter。这将返回一个HttpUnauthorizedResult。 This result is correctly setting the statusCode to 401. The problem, as i understand it, is that asp.net is intercepting the response (since its taged as a 401) and redirecting to the login page instead of just letting it go through. 这个结果正确地将statusCode设置为401.正如我所理解的那样,问题是asp.net正在拦截响应(因为它被调为401)并重定向到登录页面而不是让它通过。 I want to bypass this interception for certain urls. 我想绕过某些网址拦截。

Ok, I worked around this. 好的,我解决了这个问题。 I made a custom ActionResult (HttpForbiddenResult) and custom ActionFilter (NoFallBackAuthorize). 我做了一个自定义ActionResult(HttpForbiddenResult)和自定义ActionFilter(NoFallBackAuthorize)。

To avoid redirection, HttpForbiddenResult marks responses with status code 403. FormsAuthentication doesn't catch responses with this code so the login redirection is effectively skipped. 为了避免重定向,HttpForbiddenResult使用状态代码403标记响应.FormsAuthentication不会捕获使用此代码的响应,因此有效地跳过了登录重定向。 The NoFallBackAuthorize filter checks to see if the user is authorized much like the, included, Authorize filter. NoFallBackAuthorize过滤器检查用户是否被授权,与包含的授权过滤器非常相似。 It differs in that it returns HttpForbiddenResult when access is denied. 它的不同之处在于它在拒绝访问时返回HttpForbiddenResult。

The HttpForbiddenResult is pretty trivial: HttpForbiddenResult非常简单:

public class HttpForbiddenResult : ActionResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        context.HttpContext.Response.StatusCode = 0x193; // 403
    }
}

It doesn't appear to be possible to skip the login page redirection in the FormsAuthenticationModule. 似乎无法跳过FormsAuthenticationModule中的登录页面重定向。

Might be a kludge (and may not even work) but on your Login page see if Request.QueryString["ReturnUrl"] != null and if so set Response.StatusCode = 401 . 可能是一个kludge(甚至可能不工作),但在你的登录页面上看看Request.QueryString["ReturnUrl"] != null如果是这样设置Response.StatusCode = 401

Bear in mind that you'll still need to get your console app to authenticate somehow. 请记住,您仍然需要让您的控制台应用程序以某种方式进行身份验证。 You don't get HTTP basic auth for free: you have to roll your own, but there are plenty of implementations about. 您没有免费获得HTTP基本身份验证:您必须自己动手,但有很多实现。

Did you write your own FormsAuth attribute for the action? 您是否为该操作编写了自己的FormsAuth属性? If so, in the OnActionExecuting method, you get passed the FilterExecutingContext. 如果是这样,在OnActionExecuting方法中,您将传递FilterExecutingContext。 You can use this to pass back the 401 code. 您可以使用它来传回401代码。

public class FormsAuth : ActionFilterAttribute
{
    public override void OnActionExecuting(FilterExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.StatusCode = 401;
        filterContext.Cancel = true;
    }
}

This should work. 这应该工作。 I am not sure if you wrote the FormsAuth attribute or if you got it from somewhere else. 我不确定你是否编写了FormsAuth属性,或者你是否从其他地方获得了它。

I haven't used the AuthorizeAttribute that comes in Preview 4 yet. 我还没有使用预览版4中的AuthorizeAttribute。 I rolled my own, because I have been using the MVC framework since the first CTP. 我自己动手,因为自从第一个CTP以来我一直在使用MVC框架。 I took a quick look at the attribute in reflector and it is doing what I mentioned above internally, except they use the hex equivalent of 401. I will need to look further up the call, to see where the exception is caught, because more than likely that is where they are doing the redirect. 我快速查看了反射器中的属性,它正在内部执行我上面提到的,除了它们使用相当于401的十六进制。我将需要进一步查看调用,以查看异常被捕获的位置,因为超过可能是他们正在进行重定向的地方。 This is the functionality you will need to override. 这是您需要覆盖的功能。 I am not sure if you can do it yet, but I will post back when I find it and give you a work around, unless Haacked sees this and posts it himself. 我不确定你是否可以这样做,但是当我找到它并给你一个解决方法时我会回复,除非Haacked看到并自己发布。

I did some googling and this is what I came up with: 我做了一些谷歌搜索,这就是我提出的:


    HttpContext.Current.Response.StatusCode = 401;

Not sure if it works or not, I haven't tested it. 不确定它是否有效,我还没有测试过。 Either way, it's worth a try, right? 无论哪种方式,都值得一试,对吗? :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM