zoukankan      html  css  js  c++  java
  • 在mvc3中经常使用身份验证实现

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using PetsStore.Models;
    using PetsStore.EntitiesRepositories;

    namespace PetsStore.Filters
    {
        public class AdminAttribute:AuthorizeAttribute
        {
            private UserRepository userRepository = new UserRepository();
            protected override bool AuthorizeCore(HttpContextBase httpContext)
            {
                if (!httpContext.User.Identity.IsAuthenticated)
                {
                    return false;
                }
                var userName=httpContext.User.Identity.Name;
                User user = userRepository.GetByUserName(userName);
                if (!Roles.Contains(user.Role.RoleName))
                {
                    return false;
                }
                return true;
            }
            protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
            {
                if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
                {
                    filterContext.Result = new RedirectResult(string.Format("/Account/AdminLogin?ReturnUrl={0}/{1}", filterContext.RouteData.Values["controller"], filterContext.RouteData.Values["action"]));
                }
                else
                {
                    filterContext.Result = new ContentResult() { Content = "对不起,您的权限不足!" };
                }

            }
        }
    }

  • 相关阅读:
    [Effective C++, 学习总结] 01 视C++为一个语言联邦
    【原创】从“心”开始
    [C++, Basic, 02] 控制对象初始化与析构的顺序
    电信PPPoE拨号失败,获取不到IP
    IPV6学习笔记
    win10提示目前无法访问SmartScreen
    IBM服务器进入IMM
    python把文字转成语音
    python爬虫获取贴吧图片
    ibm x3550更换主板后无法加载系统引导
  • 原文地址:https://www.cnblogs.com/yxlblogs/p/3050855.html
Copyright © 2011-2022 走看看