0

我有一个模块旨在使管理员能够管理用户。当然,这个模块不仅需要认证,还需要特定的声明。我发现,如果您遗漏了相关索赔,您实际上只会得到一个空白页作为对您请求的响应。这并不理想。

我该如何改变呢?

下面的模块代码(如果有人需要看)...

public class UserModule : NancyModule
{
    public UserModule()
        : base("/users")
    {
        this.RequiresAnyClaim(new[] { "evil-dictator" });

        Get["/"] = _ =>
        {
            ViewBag.UserName = Context.CurrentUser.UserName;
            return Negotiate.WithView("Index");
        };

        // Generate an invitation for a pre-approved user
        Get["/invite"] = _ =>
        {
            throw new NotImplementedException();
        };
    }
}
4

1 回答 1

0

如果缺少声明,您可以使用After挂钩来更改响应。请注意,当您没有所需声明时,您获得的响应具有 HTTP 状态代码403 Forbidden。检查After钩子中的内容并根据需要更改响应。

"/"例如,当用户确实拥有邪恶的独裁者声明时,以下内容将重定向到应用程序的根目录:

public class UserModule : NancyModule
{
    public UserModule()
        : base("/users")
    {
        After += context =>
        {
            if (ctx.Response.StatusCode == HttpStatusCode.Forbidden)
              ctx.Response = this.Response.AsRedirect("/");
        }

        this.RequiresAnyClaim(new[] { "evil-dictator" });

        Get["/"] = _ =>
        {
            ViewBag.UserName = Context.CurrentUser.UserName;
            return Negotiate.WithView("Index");
        };

        // Generate an invitation for a pre-approved user
       Get["/invite"] = _ =>
       {
            throw new NotImplementedException();
       };
    }
}
于 2014-03-29T07:38:43.803 回答