コード例 #1
0
 /// <summary>
 /// Initializes this provider's base settings.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="config"></param>
 public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
 {
     base.Initialize(name, config);
     IsObsolete   = config.GetBooleanValue("IsObsolete", false);
     SaltModifier = config.GetStringValue("SaltModifier", null); //none by default.
 }
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            if (String.IsNullOrEmpty(name))
            {
                name = "EFMembership";
            }

            config.Remove("description");
            config.Add("description", "Ado.net Entity Framework Membership Provider");
            base.Initialize(name, config);

            TablePrefix            = config.GetStringValue("tablePrefix", "");
            UserMembershipProvider = config.GetStringValue("membershipProvider", string.Empty);

            MembershipProvider membership;

            if (string.IsNullOrWhiteSpace(UserMembershipProvider))
            {
                membership = Membership.Provider;
            }
            else
            {
                membership = Membership.Providers[UserMembershipProvider];
            }

            if (membership == null)
            {
                throw new ProviderException("Membership_provider_not_found".Resource());
            }

            var memType = membership.GetType();

#if USE_WEBMATRIX
            var memDefType = typeof(EFMembershipProvider <, ,>);
#else
            var memDefType = typeof(EFMembershipProvider <,>);
#endif

            var tmpType = memType;
            var isType  = false;
            while (tmpType != null)
            {
                var myTmp = default(Type);
                if (tmpType.IsGenericType)
                {
                    myTmp = tmpType.GetGenericTypeDefinition();
                }
                else
                {
                    myTmp = tmpType;
                }

                if (myTmp.IsSubclassOf(memDefType))
                {
                    isType = true;
                    break;
                }
                if (myTmp.IsAssignableFrom(memDefType))
                {
                    isType = true;
                    break;
                }
                if (myTmp.IsInstanceOfType(memDefType))
                {
                    isType = true;
                    break;
                }
                if (myTmp == memDefType)
                {
                    isType = true;
                    break;
                }
                tmpType = tmpType.BaseType;
            }

            if (!isType)
            {
                throw new ProviderException("Membership_provider_must_be_type_of_EFMembershipProvider".Resource());
            }

            //if (memDefType.Module != memType.Module)
            //    throw new ProviderException("Membership_provider_must_be_type_of_EFMembershipProvider".Resource());
            //if (!memDefType.Name.Equals(memType.Name))
            //    throw new ProviderException("Membership_provider_must_be_type_of_EFMembershipProvider".Resource());

            string temp = config["connectionStringName"];
            if (temp == null || temp.Length < 1)
            {
                throw new ProviderException("Connection_name_not_specified".Resource());
            }
            if (System.Configuration.ConfigurationManager.ConnectionStrings[temp] == null)
            {
                throw new ProviderException("Connection_not_found".Resource());
            }
            ConnectionString = temp;

            ApplicationName = config.GetStringValue("applicationName", "/");
            if (ApplicationName.Length > 256)
            {
                throw new ProviderException("Provider_application_name_too_long".Resource());
            }

            var type = typeof(TRole);
            var attr = type.GetCustomAttributes(typeof(EFDataMapperAttribute), false);

            RoleHelper = new Mapper.ClassHelper <TRole, Mapper.RoleColumnType, RoleColumnAttribute>(TablePrefix, TableSchema);
            if (attr != null && attr.Length > 0)
            {
                var mapperAttr = attr[0] as EFDataMapperAttribute;
                RoleMapper = Activator.CreateInstance(mapperAttr.MapperType) as Mapper.IRoleMapper <TRole>;

                if (RoleMapper == null)
                {
                    throw new ProviderException("Reflection_can_not_cast_object".Resource(mapperAttr.MapperType.FullName,
                                                                                          typeof(Mapper.IUserMapper <TRole>).FullName));
                }
            }
            else
            {
                RoleMapper = new Mapper.RoleAutoMapper <TRole>(RoleHelper);
            }

            if (!RoleHelper.Properties.ContainsKey(Mapper.RoleColumnType.RoleName))
            {
                throw new ProviderException("Reflection_property_is_required".Resource("RoleName"));
            }
            if (!RoleHelper.Properties.ContainsKey(Mapper.RoleColumnType.RoleID))
            {
                throw new ProviderException("Reflection_property_is_required".Resource("RoleID"));
            }

            type = typeof(TUserRole);
            attr = type.GetCustomAttributes(typeof(EFDataMapperAttribute), false);

            UserRoleHelper = new Mapper.ClassHelper <TUserRole, Mapper.UserRoleColumnType, UserRoleColumnAttribute>(TablePrefix, TableSchema);
            if (attr != null && attr.Length > 0)
            {
                var mapperAttr = attr[0] as EFDataMapperAttribute;
                UserRoleMapper = Activator.CreateInstance(mapperAttr.MapperType) as Mapper.IUserRoleMapper <TUserRole>;

                if (UserRoleMapper == null)
                {
                    throw new ProviderException("Reflection_can_not_cast_object".Resource(mapperAttr.MapperType.FullName,
                                                                                          typeof(Mapper.IUserMapper <TUserRole>).FullName));
                }
            }
            else
            {
                UserRoleMapper = new Mapper.UserRoleAutoMapper <TUserRole>(UserRoleHelper);
            }

            var generics   = tmpType.GetGenericArguments();
            var checkIndex = 1;
#if USE_WEBMATRIX
            checkIndex = 2;
#endif
            if (generics[checkIndex] != UserRoleHelper.Get(Mapper.UserRoleColumnType.UserID).PropertyType)
            {
                throw new ProviderException("Reflection_UserID_type_is_not_match".Resource());
            }

            if (!UserRoleHelper.Properties.ContainsKey(Mapper.UserRoleColumnType.UserID))
            {
                throw new ProviderException("Reflection_property_is_required".Resource("UserID"));
            }
            if (!UserRoleHelper.Properties.ContainsKey(Mapper.UserRoleColumnType.RoleID))
            {
                throw new ProviderException("Reflection_property_is_required".Resource("RoleID"));
            }

            SupportApplication = RoleHelper.Properties.ContainsKey(Security.Mapper.RoleColumnType.Application);
            SupportCreateOn    = RoleHelper.Properties.ContainsKey(Security.Mapper.RoleColumnType.CreateOn);

            var getUserMethod1 = memType.GetMethod("GetUserID", new Type[] { typeof(string) }, null);
            var getUserMethod2 = memType.GetMethod("GetUserID", new Type[] { typeof(string[]) }, null);
            Func <string, object>        getUser1 = username => getUserMethod1.Invoke(membership, new object[] { username });
            Func <string[], IEnumerable> getUser2 = username => getUserMethod2.Invoke(membership, new object[] { username }) as IEnumerable;

            var getUsernameMethod1 = memType.GetMethod("GetUsername", new Type[] { generics[1] }, null);
            var getUsernameMethod2 = memType.GetMethod("GetUsername", new Type[] { typeof(object).MakeArrayType() }, null);
            Func <object, string>        getUsername1 = userid => getUsernameMethod1.Invoke(membership, new object[] { userid }) as string;
            Func <IEnumerable, string[]> getUsername2 = userid => getUsernameMethod2.Invoke(membership, new object[] { userid }) as string[];

            GetUserID      = getUser1;
            GetUserIDArray = getUser2;

            GetUsername      = getUsername1;
            GetUsernameArray = getUsername2;

            attr = type.GetCustomAttributes(typeof(EFDataContextAttribute), false);
            if (attr != null && attr.Length > 0)
            {
                var contextAttr = attr[0] as EFDataContextAttribute;
                Context = Activator.CreateInstance(contextAttr.ContextType) as DataContext.IRoleContext <TRole, TUserRole, TRoleKey>;
                if (Context == null)
                {
                    throw new ProviderException("Reflection_can_not_cast_object".Resource(contextAttr.ContextType.FullName,
                                                                                          typeof(DataContext.IRoleContext <TRole, TUserRole, TRoleKey>).FullName));
                }
            }
            else
            {
                Context = new DataContext.DefaultRoleContext <TRole, TUserRole, TRoleKey>(RoleHelper, UserRoleHelper);
            }
            Context.Initialize(this);
        }
コード例 #3
0
 /// <summary>
 /// Initializes the worker task with the specified configuration.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="configValue"></param>
 public override void Initialize(string name, System.Collections.Specialized.NameValueCollection configValue)
 {
     ConnectionName           = configValue.GetStringValue("connectionName", null);
     MessageFailedSkipSeconds = configValue.GetDoubleValue("MessageExceptionSkipSeconds", 0.0);
     base.Initialize(name, configValue);
 }