MVC自定义身份验证、授权和角色实现

14
请耐心等待,我将提供有关该问题的详细信息...
我有一个使用FormsAuthentication和自定义服务类进行身份验证、授权、角色/成员资格等的MVC站点。
身份验证
有三种登录方式:(1)电子邮件+别名(2) OpenID(3)用户名+密码。所有这三种方式都会为用户生成一个身份验证cookie并启动一个会话。前两种方式仅由访问者使用(仅限会话),而第三种方式则用于具有数据库帐户的作者/管理员。
public class BaseFormsAuthenticationService : IAuthenticationService
{
    // Disperse auth cookie and store user session info.
    public virtual void SignIn(UserBase user, bool persistentCookie)
    {
        var vmUser = new UserSessionInfoViewModel { Email = user.Email, Name = user.Name, Url = user.Url, Gravatar = user.Gravatar };

        if(user.GetType() == typeof(User)) {
            // roles go into view model as string not enum, see Roles enum below.
            var rolesInt = ((User)user).Roles;
            var rolesEnum = (Roles)rolesInt;
            var rolesString = rolesEnum.ToString();
            var rolesStringList = rolesString.Split(',').Select(role => role.Trim()).ToList();
            vmUser.Roles = rolesStringList;
        }

        // i was serializing the user data and stuffing it in the auth cookie
        // but I'm simply going to use the Session[] items collection now, so 
        // just ignore this variable and its inclusion in the cookie below.
        var userData = "";

        var ticket = new FormsAuthenticationTicket(1, user.Email, DateTime.UtcNow, DateTime.UtcNow.AddMinutes(30), false, userData, FormsAuthentication.FormsCookiePath);
        var encryptedTicket = FormsAuthentication.Encrypt(ticket);
        var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket) { HttpOnly = true };
        HttpContext.Current.Response.Cookies.Add(authCookie);
        HttpContext.Current.Session["user"] = vmUser;
    }
}

角色

一个用于权限的简单标志位枚举:

[Flags]
public enum Roles
{
    Guest = 0,
    Editor = 1,
    Author = 2,
    Administrator = 4
}

枚举扩展,帮助枚举标志位(哇!)的枚举。

public static class EnumExtensions
{
    private static void IsEnumWithFlags<T>()
    {
        if (!typeof(T).IsEnum)
            throw new ArgumentException(string.Format("Type '{0}' is not an enum", typeof (T).FullName));
        if (!Attribute.IsDefined(typeof(T), typeof(FlagsAttribute)))
            throw new ArgumentException(string.Format("Type '{0}' doesn't have the 'Flags' attribute", typeof(T).FullName));
    }

    public static IEnumerable<T> GetFlags<T>(this T value) where T : struct
    {
        IsEnumWithFlags<T>();
        return from flag in Enum.GetValues(typeof(T)).Cast<T>() let lValue = Convert.ToInt64(value) let lFlag = Convert.ToInt64(flag) where (lValue & lFlag) != 0 select flag;
    }
}

授权

服务提供了检查经过身份验证的用户角色的方法。

public class AuthorizationService : IAuthorizationService
{
    // Convert role strings into a Roles enum flags using the additive "|" (OR) operand.
    public Roles AggregateRoles(IEnumerable<string> roles)
    {
        return roles.Aggregate(Roles.Guest, (current, role) => current | (Roles)Enum.Parse(typeof(Roles), role));
    }

    // Checks if a user's roles contains Administrator role.
    public bool IsAdministrator(Roles userRoles)
    {
        return userRoles.HasFlag(Roles.Administrator);
    }

    // Checks if user has ANY of the allowed role flags.
    public bool IsUserInAnyRoles(Roles userRoles, Roles allowedRoles)
    {
        var flags = allowedRoles.GetFlags();
        return flags.Any(flag => userRoles.HasFlag(flag));
    }

    // Checks if user has ALL required role flags.
    public bool IsUserInAllRoles(Roles userRoles, Roles requiredRoles)
    {
        return ((userRoles & requiredRoles) == requiredRoles);
    }

    // Validate authorization
    public bool IsAuthorized(UserSessionInfoViewModel user, Roles roles)
    {
        // convert comma delimited roles to enum flags, and check privileges.
        var userRoles = AggregateRoles(user.Roles);
        return IsAdministrator(userRoles) || IsUserInAnyRoles(userRoles, roles);
    }
}

我选择通过属性在我的控制器中使用这个:

public class AuthorizationFilter : IAuthorizationFilter
{
    private readonly IAuthorizationService _authorizationService;
    private readonly Roles _authorizedRoles;

    /// <summary>
    /// Constructor
    /// </summary>
    /// <remarks>The AuthorizedRolesAttribute is used on actions and designates the 
    /// required roles. Using dependency injection we inject the service, as well 
    /// as the attribute's constructor argument (Roles).</remarks>
    public AuthorizationFilter(IAuthorizationService authorizationService, Roles authorizedRoles)
    {
        _authorizationService = authorizationService;
        _authorizedRoles = authorizedRoles;
    }

    /// <summary>
    /// Uses injected authorization service to determine if the session user 
    /// has necessary role privileges.
    /// </summary>
    /// <remarks>As authorization code runs at the action level, after the 
    /// caching module, our authorization code is hooked into the caching 
    /// mechanics, to ensure unauthorized users are not served up a 
    /// prior-authorized page. 
    /// Note: Special thanks to TheCloudlessSky on StackOverflow.
    /// </remarks>
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        // User must be authenticated and Session not be null
        if (!filterContext.HttpContext.User.Identity.IsAuthenticated || filterContext.HttpContext.Session == null)
            HandleUnauthorizedRequest(filterContext);
        else {
            // if authorized, handle cache validation
            if (_authorizationService.IsAuthorized((UserSessionInfoViewModel)filterContext.HttpContext.Session["user"], _authorizedRoles)) {
                var cache = filterContext.HttpContext.Response.Cache;
                cache.SetProxyMaxAge(new TimeSpan(0));
                cache.AddValidationCallback((HttpContext context, object o, ref HttpValidationStatus status) => AuthorizeCache(context), null);
            }
            else
                HandleUnauthorizedRequest(filterContext);             
        }
    }

我在控制器中使用此属性来装饰操作,就像微软的[Authorize]一样,没有参数意味着允许任何已认证的人进入 (对于我来说,枚举=0,没有必需的角色)。

以上是背景信息(呼...),写下这些内容后我回答了我的第一个问题。此时,我想知道我的设置是否合适:

  1. 我需要手动获取身份验证 cookie 并填充 HttpContext 的 FormsIdentity 主体,还是应该自动完成?

  2. 在属性/过滤器 OnAuthorization() 中检查身份验证是否有任何问题?

  3. 使用 Session[] 存储我的视图模型与在身份验证 cookie 中序列化它之间的权衡是什么?

  4. 这个解决方案是否足够遵循“关注点分离”的理念?(作为一个更多基于观点的问题,这是额外的奖励)

3个回答

8

虽然我认为您在这个方面做得很好,但我不明白您为什么要重复造轮子。微软提供了一个名为Membership和Role Providers的系统用于此目的。您为什么不只写一个自定义的成员资格和角色提供程序,然后就可以使用内置的授权属性和/或过滤器,而不必创建自己的。


1
@insta> 这就是为什么你应该扩展 Provider 接口,而不是使用现有的接口。如果不遵循现有的接口,他将错过许多支持它们的内置工具。 - Paul
1
@Paul:MVC中的许多连线都期望抽象基类,而不是它们通常甚至不实现的接口。 - Bryan Boettcher
1
@one.beat.consumer - 你没有得到任何回应的一个原因是因为你的做法与其他人不同。换句话说,你在逆流而上。我个人认为你让这件事变得比必要的难度高了10倍。 - Erik Funkenbusch
1
@one.beat.consumer - 我并不是要告诉你不要问问题,而是解释为什么你没有得到回复。此外,会员制度与Oracle、Raven、Mongo等数据库完全兼容,但你可能需要自己实现提供程序。这也是会员提供程序存在的全部原因,让你可以插入任何你想要的后端。 - Erik Funkenbusch
@MystereMan,我已经授予了悬赏,因为我对这个系统还很陌生,而且它快要到期了。感谢您的帮助,但我会暂时不标记这个问题,希望能得到更多的答案。再次感谢。 - one.beat.consumer
显示剩余3条评论

8
我的CodeReview答案转载:
我会尝试回答你的问题并提供一些建议:
  1. 如果在web.config中配置了FormsAuthentication,它将自动为您提取cookie,因此您不需要手动填充FormsIdentity。无论如何,这很容易测试。
  2. 对于有效的授权属性,您可能希望覆盖AuthorizeCoreOnAuthorizationAuthorizeCore方法返回布尔值,并用于确定用户是否可以访问给定资源。 OnAuthorization不返回通常用于基于身份验证状态触发其他事情。
  3. 我认为会话与cookie的问题在很大程度上是个人喜好,但我建议出于几个原因使用会话。最大的原因是cookie随每个请求传输,虽然现在您可能只有一点数据,但随着时间的推移,谁知道您会把什么东西塞进去。添加加密开销后,它可能变得足够大,以减慢请求速度。将其存储在会话中也将数据所有权放在您手中(而不是将其放在客户端手中并依靠您解密和使用它)。我建议的一个建议是将该会话访问封装在静态的UserContext类中,类似于HttpContext,这样您只需进行调用,例如UserContext.Current.UserData。请参见下面的示例代码。
  4. 我无法真正说出它是否是关注点的良好分离,但对我来说它看起来是一个很好的解决方案。实际上,这与我在应用程序中使用的其他MVC身份验证方法非常相似。
最后一个问题-为什么要手动构建并设置FormsAuthentication cookie,而不是使用FormsAuthentication.SetAuthCookie?只是好奇。
静态上下文类的示例代码:
public class UserContext
{
    private UserContext()
    {
    }

    public static UserContext Current
    {
        get
        {
            if (HttpContext.Current == null || HttpContext.Current.Session == null)
                return null;

            if (HttpContext.Current.Session["UserContext"] == null)
                BuildUserContext();

            return (UserContext)HttpContext.Current.Session["UserContext"];
        }
    }

    private static void BuildUserContext()
    {
        BuildUserContext(HttpContext.Current.User);
    }

    private static void BuildUserContext(IPrincipal user)
    {
        if (!user.Identity.IsAuthenticated) return;

        // For my application, I use DI to get a service to retrieve my domain
        // user by the IPrincipal
        var personService = DependencyResolver.Current.GetService<IUserBaseService>();
        var person = personService.FindBy(user);

        if (person == null) return;

        var uc = new UserContext { IsAuthenticated = true };

        // Here is where you would populate the user data (in my case a SiteUser object)
        var siteUser = new SiteUser();
        // This is a call to ValueInjecter, but you could map the properties however
        // you wanted. You might even be able to put your object in there if it's a POCO
        siteUser.InjectFrom<FlatLoopValueInjection>(person);

        // Next, stick the user data into the context
        uc.SiteUser = siteUser;

        // Finally, save it into your session
        HttpContext.Current.Session["UserContext"] = uc;
    }


    #region Class members
    public bool IsAuthenticated { get; internal set; }
    public SiteUser SiteUser { get; internal set; }

    // I have this method to allow me to pull my domain object from the context.
    // I can't store the domain object itself because I'm using NHibernate and
    // its proxy setup breaks this sort of thing
    public UserBase GetDomainUser()
    {
        var svc = DependencyResolver.Current.GetService<IUserBaseService>();
        return svc.FindBy(ActiveSiteUser.Id);
    }

    // I have these for some user-switching operations I support
    public void Refresh()
    {
        BuildUserContext();
    }

    public void Flush()
    {
        HttpContext.Current.Session["UserContext"] = null;
    }
    #endregion
}

过去,我直接在UserContext类上设置属性以访问所需的用户数据,但随着我将其用于其他更复杂的项目,我决定将其移动到SiteUser类中:

public class SiteUser
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName
    {
        get { return FirstName + " " + LastName; }
    }
    public string AvatarUrl { get; set; }

    public int TimezoneUtcOffset { get; set; }

    // Any other data I need...
}

谢谢您转发这个答案,更感谢您提出的UserContext想法。也许我们可以有时间聊一下或者通过邮件交流? - one.beat.consumer

1

你的MVC自定义身份验证、授权和角色实现看起来不错。回答你的第一个问题,当你不使用MembershipProvider时,你必须自己填充FormsIdentity principal。我使用的解决方案在这里描述我的博客


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接