locked
Membership with MySql RRS feed

  • Question

  • User692200542 posted

    Hi,

    I been trying to configure membership with my sql. But there seems to be some problem. Could you help me to solve it?

    it gives this error while i click on the security tab on the configuration file

    "There is a problem with your selected data store. This can be caused by an invalid server name or credentials, or by insufficient permission. It can also be caused by the role manager feature not being enabled. Click the button below to be redirected to a page where you can choose a new data store.

    The following message may help in diagnosing the problem: The source was not found, but some or all event logs could not be searched. To create the source, you need permission to read all event logs to make sure that the new source name is unique. Inaccessible logs: Security."

    Sunday, August 14, 2011 9:03 AM

All replies

  • User692200542 posted

    @huske:

    i already tried this ... After this it is giving me an error ... dont know where the problem is ... i also referred to articles on CodeProject but dont knw what kinda isssue this is ...

    Sunday, August 14, 2011 9:24 AM
  • User-3972072 posted

    I have been using this configuration in my Web.config (please edit it to fit your version of the MySQL Connector)

    <configuration>
      <connectionStrings>
        <add name="ApplicationServices" connectionString="server=SERVER.COM;database=DATABASE_NAME;uid=USERNAME;pwd=PASSWORD;" providerName="MySql.Data.MySqlClient" />
      </connectionStrings>
      <system.data>
        <DbProviderFactories>
          <remove invariant="MySql.Data.MySqlClient" />
          <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.3.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
        </DbProviderFactories>
      </system.data>
      <system.web>
        <authentication mode="Forms">
          <forms loginUrl="~/Account/LogOn" timeout="2880" path="/" cookieless="UseCookies" slidingExpiration="true" />
        </authentication>
        <membership defaultProvider="MySQLMembershipProvider">
          <providers>
            <clear />
            <add connectionStringName="ApplicationServices" applicationName="APPNAME" name="MySQLMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.3.6.0" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Clear" maxInvalidPasswordAttempts="15" minRequiredPasswordLength="1" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="30" passwordStrengthRegularExpression="" />
          </providers>
        </membership>
        <profile defaultProvider="MySQLProfileProvider" enabled="true" automaticSaveEnabled="false" inherits="MyApp.Models.AccountProfile">
          <providers>
            <clear />
            <add connectionStringName="ApplicationServices" applicationName="APPNAME" name="MySQLProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider, MySql.Web, Version=6.3.6.0" />
          </providers>
        </profile>
        <roleManager defaultProvider="MySQLRoleProvider" enabled="true" cacheRolesInCookie="true">
          <providers>
            <remove name="MySQLRoleProvider" />
            <add connectionStringName="ApplicationServices" applicationName="APPNAME" name="MySQLRoleProvider" type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.3.6.0" />
          </providers>
        </roleManager>
      </system.web>
    </configuration>

    Then I use the following model "AccountProfile.cs":

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Security;
    using System.Web.Profile;
    
    namespace MyApp.Models
    {
        public class AccountProfile : ProfileBase
        {
            [SettingsAllowAnonymous(false)]
            public string Name
            {
                get { return base["Name"] as string; }
                set { base["Name"] = value; }
            }
    
            [SettingsAllowAnonymous(false)]
            public string Address
            {
                get { return base["Address"] as string; }
                set { base["Address"] = value; }
            }
    
            [SettingsAllowAnonymous(false)]
            public string Description
            {
                get { return base["Description"] as string; }
                set { base["Description"] = value; }
            }
    
            public static int GetUserID(string username)
            {
                return (int)Membership.GetUser(username).ProviderUserKey;
            }
    
            public static int GetUserID()
            {
                return (int)Membership.GetUser().ProviderUserKey;
            }
    
            public static string GetUserEmail(string username)
            {
                MembershipUser mu = Membership.GetUser(username);
                if (mu != null)
                    return mu.Email;
                else
                    return "[ERROR]";
            }
    
            public static string GetUserEmail(object providerUserKey)
            {
                MembershipUser mu = Membership.GetUser(providerUserKey);
                if (mu != null)
                    return mu.Email;
                else
                    return "[ERROR]";
            }
    
            public static string GetUserEmail()
            {
                return Membership.GetUser().Email;
            }
    
            public static AccountProfile GetUserProfile(string username)
            {
                return Create(username) as AccountProfile;
            }
    
            public static AccountProfile GetUserProfile(object providerUserKey)
            {
                MembershipUser mu = Membership.GetUser(providerUserKey);
                if (mu != null)
                    return Create(mu.UserName) as AccountProfile;
                else
                    return new AccountProfile();
            }
    
            public static AccountProfile GetUserProfile()
            {
                MembershipUser mu = Membership.GetUser();
                if (mu != null)
                    return Create(mu.UserName) as AccountProfile;
                else
                    return new AccountProfile();
            }
        }
    }

    I then use this code in a controller to access a ProfileAccount ViewModel that I created. But anyway here is how to get the AccountProfile:

                AccountProfile profile = AccountProfile.GetUserProfile(User.Identity.Name);
                var profileModel = new AccountProfileViewModel
                {
                    Name = profile.Name,
                    Address = profile.Address,
                    Description = profile.Description,
                    Email = AccountProfile.GetUserEmail(profile.UserName),
                    UserName = profile.UserName,
                    UserID = AccountProfile.GetUserID(profile.UserName).ToString()
                };

    Hope this helps.

    Sunday, August 14, 2011 3:17 PM