asp.net mvc中权限判断失败时跳转页 ReturnUrl

本文详细解析了如何使用AdminAuthorizeAttribute实现基于角色的授权机制,并在web.config中进行配置,确保只有经过身份验证的用户才能访问特定方法或类。重点介绍了配置过程中的关键步骤和返回结果的处理,包括返回指定的登录页面。同时,阐述了配置后的URL路径,即当未登录用户尝试访问受保护资源时,系统会引导其至登录页面并附带返回URL参数。
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
    public class AdminAuthorizeAttribute : FilterAttribute, IAuthorizationFilter
    {
        private readonly bool _isLogin;
        public AdminAuthorizeAttribute(bool  isLogin)
        {
            _isLogin = isLogin;
        }

        public void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext == null)
                throw new ArgumentNullException("filterContext");

            if (OutputCacheAttribute.IsChildActionCacheActive(filterContext))
                throw new InvalidOperationException(
                    "You cannot use [MemberAuthorize] attribute when a child action cache is active");

            if (_isLogin)
            {
                filterContext.Result = new HttpUnauthorizedResult();
            }
        }
    }

红色地方标注的就是返回结果

该结果会在web.config中进行配置  都会加上ReturnUrl

<authentication mode="Forms">
      <forms loginUrl="~/Test/Login" timeout="2880" />
</authentication>

配置后返回结果:/Test/Login?ReturnUrl=%2fTest%2fTestKindEditor
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值