有没有办法扩展ASP.NET成员资格?

时间:2011-05-07 16:51:12

标签: .net asp.net asp.net-mvc asp.net-membership

我想扩展静态类public static class Membershippublic abstract class MembershipProvider有什么方法可以做到吗?

2 个答案:

答案 0 :(得分:5)

有关如何通过继承MembershipProvider创建自定义成员资格提供程序的完整详细说明,请参阅MSDN上的Implementing a Membership Provider。至于静态Membership类,没有必要扩展它;你不能覆盖静态方法。它只是委托给MembershipProvider的实例,所以你不想扩展Membership类,你只想设置你的配置,以便调用你的新MembershipProvider。

答案 1 :(得分:0)

对于那些想要扩展现有提供商的人,例如:

using System;
using System.Web;
using System.Web.Security;

namespace System.Web.MVC_Extensions
{
    public static class SqlMembershipProviderExtensions
    {
        public static string GenerateAccessToken(this MembershipProvider provider, string username, int expiresInMinutes)
        {
            if (provider.GetType().Name != "SqlMembershipProvider")
                throw new NotSupportedException("Access tokens only supported by SqlMembershipProvider.");

            ...
        }

        public static bool ValidateAccessToken(this MembershipProvider provider, string username, string token)
        {
            if (provider.GetType().Name != "SqlMembershipProvider")
                throw new NotSupportedException("Access tokens only supported by SqlMembershipProvider.");

            ...
        }
    }
}