예제 #1
0
        /// <summary>
        /// Gets the profile settings for the current user from the <see cref="ProfileBase"/>
        /// and sets them into the specified <paramref name="user"/>.
        /// </summary>
        /// <remarks>
        /// The <paramref name="user"/> is updated from the profile using the following algorithm.
        /// <para>
        /// For every property in <paramref name="user"/>:
        ///  if (the property can be set and is in the profile)
        ///   then set the property value using the value in the profile specified by the alias
        /// </para>
        /// </remarks>
        /// <param name="user">The user to update with the profile settings</param>
        /// <exception cref="InvalidDataContractException"> is thrown if a property in
        /// <paramref name="user"/> that meets the specified conditions does not have a
        /// corresponding profile value.
        /// </exception>
        private static void GetProfile(T user)
        {
            if (string.IsNullOrEmpty(user.Name) || !ProfileManager.Enabled)
            {
                return;
            }

            // We're creating a new profile so this algorithm works with both Login and
            // Logout where the current principal and Profile have not been updated.
            ProfileBase profile = ProfileBase.Create(user.Name);

            foreach (PropertyInfo property in user.GetType().GetProperties())
            {
                if (!property.CanWrite || !AuthenticationBase <T> .IsInProfile(property) || property.GetIndexParameters().Length > 0)
                {
                    // Skip this property if it is not writable or in the profile or is an indexer property
                    continue;
                }

                try
                {
                    property.SetValue(user, profile.GetPropertyValue(AuthenticationBase <T> .GetProfileAlias(property)), null);
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    // The default ASP.NET providers use SQL. Since these errors are sometimes
                    // hard to interpret, we're wrapping them to provide more context.
                    throw new DomainException(string.Format(
                                                  CultureInfo.InvariantCulture,
                                                  Resources.ApplicationServices_ProviderError,
                                                  "Profile", ex.Message),
                                              ex);
                }
                catch (SettingsPropertyNotFoundException e)
                {
                    throw new InvalidDataContractException(
                              string.Format(
                                  CultureInfo.InvariantCulture,
                                  Resources.ApplicationServices_ProfilePropertyDoesNotExist,
                                  AuthenticationBase <T> .GetProfileAlias(property)),
                              e);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Writes the profile settings for the current user to the <see cref="ProfileBase"/>
        /// using the specified <paramref name="user"/>.
        /// </summary>
        /// <remarks>
        /// The profile is updated from the <paramref name="user"/> using the following algorithm.
        /// <para>
        /// For every property in <paramref name="user"/>:
        ///  if (the property can be read and is in the profile)
        ///   then use the property value to set the value in the profile specified by the alias
        /// </para>
        /// </remarks>
        /// <param name="user">The user to update the profile settings with</param>
        /// <exception cref="InvalidDataContractException"> is thrown if a property in
        /// <paramref name="user"/> that meets the specified conditions does not have a
        /// corresponding profile value.
        /// </exception>
        private static void UpdateProfile(T user)
        {
            if (string.IsNullOrEmpty(user.Name) || !ProfileManager.Enabled)
            {
                return;
            }

            // We're using the current Profile since we've verified it matches the current
            // principal and it allows us to leverage the auto-save feature. When testing,
            // however, we'll need to create a new one.
            ProfileBase profile = AuthenticationBase <T> .GetProfileBase(user.Name);

            foreach (PropertyInfo property in user.GetType().GetProperties())
            {
                if (!property.CanRead ||
                    !property.CanWrite ||
                    !AuthenticationBase <T> .IsInProfile(property) ||
                    AuthenticationBase <T> .IsReadOnly(property) ||
                    property.GetIndexParameters().Length > 0)
                {
                    // Skip this property if it is not readable, in the profile, is readonly or is an indexer property
                    continue;
                }

                try
                {
                    profile.SetPropertyValue(AuthenticationBase <T> .GetProfileAlias(property), property.GetValue(user, null));
                }
                catch (SettingsPropertyNotFoundException e)
                {
                    throw new InvalidDataContractException(
                              string.Format(
                                  CultureInfo.InvariantCulture,
                                  Resources.ApplicationServices_ProfilePropertyDoesNotExist,
                                  AuthenticationBase <T> .GetProfileAlias(property)),
                              e);
                }
                catch (SettingsPropertyIsReadOnlyException e)
                {
                    throw new InvalidDataContractException(
                              string.Format(
                                  CultureInfo.InvariantCulture,
                                  Resources.ApplicationServices_ProfilePropertyReadOnly,
                                  AuthenticationBase <T> .GetProfileAlias(property)),
                              e);
                }
                catch (SettingsPropertyWrongTypeException e)
                {
                    throw new InvalidDataContractException(
                              string.Format(
                                  CultureInfo.InvariantCulture,
                                  Resources.ApplicationServices_ProfilePropertyTypeMismatch,
                                  AuthenticationBase <T> .GetProfileAlias(property)),
                              e);
                }
            }

            // Explicit invocation is necessary when auto-save is not enabled
            bool isAutoSaveEnabled = false;

            try
            {
                isAutoSaveEnabled = ProfileManager.AutomaticSaveEnabled;
            }
            catch (HttpException)
            {
                // If the feature is not supported at the current hosting permission level,
                // we can assume it is not enabled.
            }
            if (!isAutoSaveEnabled)
            {
                profile.Save();
            }
        }