using custom Profile provider in ASP.net MVC?

1.2k views Asked by At

im currently trying to implement a profile provider for my site a few days now and having a hard time working on it, im a php programer and i just shift to asp.net recently

Im using Linq to sql and follow this http://www.codeproject.com/KB/aspnet/LINQCustomProfileProvider.aspx tutorial.

the reason im using my own because i having different structure than any default of asp.net have. The profile data is inside my user table.

The compile was fine, login was fine.

but i tried

<% CMSProfile profile = HttpContext.Current.Profile as CMSProfile;%>
<%= profile.NickName %>

it won't work and throw me a System.NullReferenceException... so how can i automatically get my Profile into the HTTPCONtext so that i can call out easily everytime.

If you need any more data, i can provide.

Thank you very much.

Web.config:

<roleManager enabled="false" defaultProvider="CMSRoleProvider">
  <providers>
    <clear />
    <add name="CMSRoleProvider" type="P014.ProviderClass.CMSRoleProvider" connectionStringName="P014ConnectionString" applicationName="/" />
  </providers>
</roleManager>
2

There are 2 answers

1
Lukasz On

How have you registered the provider in web.config? You shouldn't have to instantiate the provider yourself it should be done by the app at startup. If you give more info I might be able to help.

EDIT: Here is my web.config, maybe it will be of help to you.

<profile defaultProvider="SWIntranetProfile" enabled="true">
    <providers>
        <clear/>
        <add name="SWIntranetProfile" type="SWIntranetProfile"/>
    </providers>
    <properties>
        <clear/>
        <!-- SID is really LOGON_USER -->
        <add name="SID" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="PersonID" allowAnonymous="false" type="System.Int32" readOnly="true"/>
        <add name="EmailAddress" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="Position" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="Name" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="FirstName" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="LastName" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="ImageName" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="PhoneExt" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="LastIP" allowAnonymous="false" type="System.String" readOnly="false"/>
        <add name="IntranetTheme" allowAnonymous="false" type="System.String" readOnly="false"/>
        <add name="UnionID" allowAnonymous="false" type="System.Int32" readOnly="true"/>
        <add name="UnionName" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="OfficeID" allowAnonymous="false" type="System.Int32" readOnly="true"/>
    </properties>
</profile>
5
Andy Long On

I noticed that the author of the article didn't give a code sample for binding the profiler or Workflow to the HttpContext.

Have you written your own class to do this? If so, have you set this up correctly in the web.config?

If your using IIS7 you also need to register your IHttpModule under the webServer section of the web.config file.

EDIT

To be able to run the code snippet you have show to you need to have placed your custom Profiler to the HttpContext.

You can do this in two ways, either on a per request basis or on the application start.

For the per request basis you would need to create a class that implements IHttpModule and register it in the web.config.

For the Application start you need to attach your CMSProfile to the HttpContext.Current in the Application_OnStart method.

Their is a sample application attached to the article you posted, have you downloading and checked sample application?