C# MVC授权过滤器(案例)

 public class AuthFiltersController : Controller
    {
        //[Authorize]
        /// <summary>
        /// 授权通过跳转的视图,例如:帐号密码都为a、bb、ccc通过
        /// </summary>
        /// <returns></returns>
        [Authorize(Users="a,bb,ccc")]
        public ActionResult Welcome()
        {
            return View();
        }

    }

public class LogOnViewModel //用户实例类
    {
        /// <summary>
        /// 用户名
        /// </summary>
        [DisplayName("用户名")]
        public string UserName { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
       [DisplayName("密码")]
        public string Password { get; set; }

        /// <summary>
        /// 记住我
        /// </summary>
        [DisplayName("记住我")]
        public bool RememberMe { get; set; }

    }

//必须Web.config配置设置要跳转到的视图

//在system.web节点添加跳转到,授权显示的视图

//即 FormsAuthentication.SetAuthCookie(验证字段, bool是否授权通过);true跳转视图,false跳转失败页面

<system.web>

    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="2880" />
    </authentication>
  </system.web>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值