简体   繁体   中英

asp.net mvc custom profile provider

I want to have custom profile provider in my asp.net mvc 3 app. The problem is, that I don't want to use default DB that is generated by ASP.NET Membership/Role/Profile provider, mainly because authentication is already done with WebService and DBs already exist.

I want to user profile properties to populate them and use within different areas of the site.

I took a look at this example ( How to assign Profile values? ) but I am getting this error:

An attempt to attach an auto-named database for file C:\Projects\FWManager\App_Data\aspnetdb.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Here is the web.config

  <profile inherits="FWMembership.Membership.FWProfileProvider" defaultProvider="AspNetSqlProfileProvider" automaticSaveEnabled="false" enabled="true">
   <providers>
    <clear/>
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
   </providers>
  </profile>

This is my custom class

public class FWProfileProvider : ProfileBase
{
    [SettingsAllowAnonymous(false)]
    public string FirstName
    {
        get { return base["FirstName"] as string; }
        set { base["FirstName"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    public string LastName
    {
        get { return base["LastName"] as string; }
        set { base["LastName"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    public int? UserID
    {
        get { return base["UserID"] as int?; }
        set { base["UserID"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    public string UserCompany
    {
        get { return base["UserCompany"] as string; }
        set { base["UserCompany"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    public string Email
    {
        get { return base["Email"] as string; }
        set { base["Email"] = value; }
    }

    public StringCollection Entitlements
    {
        get { return base["Entitlements"] as StringCollection; }
        set { base["Entitlements"] = value; }
    }

    public string username;

    public FWProfileProvider()
    {
    }

    public FWProfileProvider(string username)
    {
        this.username = username;
    }

    static public FWProfileProvider CurrentUser
    {
        get
        {
            return (FWProfileProvider)
                   (ProfileBase.Create("Joe"));
        }
    }

}

The key is to avoid using asp.net default membership tables. Any ideas?

EDIT: Forgot to add - this web application, but profile provider is placed in the class library project within same soulution: Solution |->FWProfile (class library project) |->UI (asp.net mvc 3 web application)

I think you have to write your own MemberShip Provider as well. Your web.config refers to the default asp.net membership provider. How to write a Membership provider you can find here custom membership provider

The default membership provider uses a connection string into a locally installed SQL Express database and that causes your error.

Your web.config would look like this:

<membership defaultProvider="MyCustomMembershipProvider">
  <providers>
    <clear />
    <add name="MyCustomMembershipProvider"
      type="FWMembership.Membership.MyCustomMembershipProvider"
      enablePasswordRetrieval="true"
      enablePasswordReset="true"
      requiresQuestionAndAnswer="false"
      requiresUniqueEmail="true"
      passwordFormat="Clear"/>
  </providers>
</membership>
<profile defaultProvider="MyProfileProvider" enabled="true">   
   <providers>    
      <clear/>    
         <add name="MyProfileProvider" type="FWMembership.Membership.FWProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />   
   </providers>  
 </profile>

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM