Tuesday, May 1, 2012

Access Membership Configurations in ASP.NET MVC Views

We define membership configurations through membership provider settings in Web.config file in an MVC application.

For example:
<membership defaultProvider="CustomMembershipProvider">
      <providers>
        <add name="CustomMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="MyApp" />
      </providers>
    </membership>

At times, you need to access these settings in your view in order to enforce some validation/ to display them on screen.
Its quite straight-forward to do. You can access them using @Membership object in your razor-view.
For example,

Application Name:<span>@Membership.ApplicationName</span>

will print - Application Name: MyApp in your view.

Similarly, you can access any of the configurations you have defined for Membership.

No comments:

Post a Comment

Thanks for visiting my blog.
However, if this helped you in any way, please take a moment to write a comment.

Thanks
Nirman