MVC授权属性仅适用于发行版 [英] MVC Authorize Attribute for Release Version only

查看:65
本文介绍了MVC授权属性仅适用于发行版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以用该类装饰类?

Is it possible to decorate the class with;

[Authorize(Roles = "Admin")]

但是它仅在以发布模式构建应用程序时才适用吗?

But have it only apply when you build the application in Release mode?

因此,在调试模式下,不应用授权,您无需登录即可进入管理页面.

So in debug mode, the authorize is not applied and you can get to the admin pages without the need to log in.

无论您是否登录,我的管理屏幕都不需要任何特殊的用户信息.

My admin screens do not require any special user information, only whether you are logged in or not.

推荐答案

一种方法是创建一个自定义Authorize属性,然后使用#if DEBUG这样:

One way is to create a custom Authorize attribute and then use #if DEBUG like this:

public class SwitchableAuthorizationAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
    bool disableAuthentication = false;

    #if DEBUG
    disableAuthentication = true;
    #endif

    if (disableAuthentication)
        return true;

    return base.AuthorizeCore(httpContext);
}
}

从这里复制: http://www .diaryofaninja.com/blog/2011/07/24/writing-your-own-custom-aspnet-mvc-authorize-attributes

这篇关于MVC授权属性仅适用于发行版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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