【问题标题】:Prevent 401 change to 302 with servicestack使用 servicestack 防止 401 更改为 302
【发布时间】:2013-09-22 09:52:18
【问题描述】:

我对 servicestack 比较陌生。我似乎在将 401 雕像改写为 302 时遇到了麻烦。我在看这个答案:

When ServiceStack authentication fails, do not redirect?

我看到建议的解决方案是添加以下内容:

Plugins.Add(new AuthFeature(...) { HtmlRedirect = null });

我的问题是,我应该在哪里添加它才能让它工作?我已经开始根据 github 上的示例构建一些东西:

public class AppHost : AppHostBase
{
    public AppHost() : base("Custom Authentication Example", typeof(AppHost).Assembly) { }

    public override void Configure(Container container)
    {
        // register storage for user sessions 
        container.Register<ICacheClient>(new MemoryCacheClient());

        // add routes
        Routes.Add<HelloRequest>("/hello"); 

        // Register AuthFeature with custom user session and custom auth provider
        Plugins.Add(new AuthFeature(
            () => new CustomUserSession(),
            new[] { new CustomCredentialsAuthProvider() }
        ));

        // Enable the metadata page
        SetConfig(new EndpointHostConfig {
            EnableFeatures = Feature.All.Add(Feature.Metadata)
        });
    }
}

非常感谢

【问题讨论】:

  • 嗨@SeanH,请问让服务器响应302而不是401是否是一个安全问题?这种变化的原因是什么?谢谢
  • @MaRco85 我不这么认为。这是一个restful API,所以使用302重定向到登录页面是没有用的。话虽如此,服务器仍然不会授予您访问所请求资源的权限,因此从这个意义上说是安全的。如果您的客户端返回 HTTP 401,您可以合理地确定存在身份验证问题并采取措施修复它。
  • 正是我的想法。非常感谢您的解释! :-) @SeanH

标签: asp.net mono servicestack http-status-codes


【解决方案1】:

你已经差不多了。

public override void Configure(Container container)
{
     Plugins.Add(new AuthFeature(() => new AuthUserSession(), new IAuthProvider[] { new BasicAuthProvider() }) { HtmlRedirect = null });

//... more config stuff...

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 2011-10-24
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 2016-11-03
    相关资源
    最近更新 更多