Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Found an answer <a href="https://stackoverflow.com/questions/12254701/simplemembershipprovider-not-working?rq=1">here by Mehdi Golchin</a> which seems to take care of:</p> <pre><code>[Authorize(Roles="admin,editor,publisher")] </code></pre> <p>If I also add this to the home controller:</p> <pre><code> [InitializeSimpleMembership] </code></pre> <p>Because this attribute is on the Accounts controller, SimpleMembership database gets initialize only after the first use of the accounts controller like login/register. Even when the current user gets logged in from the cookie, the database is not initialized and so it throws an error. One solution is to put this attribute on the home controller which gets called when I launch my Website. But, then it needs to be placed on every controller because I check roles and display different menu items based on role.</p> <p>This is poor design as the database should be initialized on the App_Start and not when first used. </p> <p>I did try to put</p> <pre><code>WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); </code></pre> <p>in the <code>Global.asax</code> <code>Application_Start()</code>, and it takes care of role checking in menu items using <code>User.IsInRole("admin")</code>, but then throw error in any controller with <code>[Authorize(Roles="admin")]</code> attribute, even with additional attribute <code>[InitializeSimpleMembership]</code> is applied.</p> <p>So right now the solution is to put `[InitializeSimpleMembership] on all controllers as a user may initially land on any page using external links.</p> <p>It still can't figure how to initialize the SimpleRolesProvider class to do more of the role management instead of just <code>User.IsInRole()</code>.</p> <p>These things do work better in the webmatrix webpages site and obviously the port ot MVC is not complete. It conflicts and gets confused with the default asp.net membership providers.</p> <p><strong>EDIT</strong> OK I was not thinking <code>[InitializeSimpleMembership]</code> filter could be applied globally by putting this line in the <code>FilterConfig.cs</code> in the App_Start folder:</p> <pre><code>filters.Add(new InitializeSimpleMembershipAttribute()); </code></pre> <p>That takes care of that problem. Now need a solution for SimpleRolesProvider initialization or else I will have to write my own roles provider.</p> <h2><strong>UPDATE:</strong></h2> <p>This <a href="http://odetocode.com/Blogs/scott/archive/2012/08/31/seeding-membership-amp-roles-in-asp-net-mvc-4.aspx" rel="nofollow noreferrer">post by Scott Allen</a> has solved all my problems.</p> <p>By including this in web.config:</p> <pre><code>&lt;roleManager enabled="true" defaultProvider="simple"&gt; &lt;providers&gt; &lt;clear/&gt; &lt;add name="simple" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/&gt; &lt;/providers&gt; &lt;/roleManager&gt; &lt;membership defaultProvider="simple"&gt; &lt;providers&gt; &lt;clear/&gt; &lt;add name="simple" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"/&gt; &lt;/providers&gt; &lt;/membership&gt; </code></pre> <p>all the methods of Roles and Membership classes become available and can be initialized in code as follows:</p> <pre><code>var roles = (SimpleRoleProvider) Roles.Provider; var membership = (SimpleMembershipProvider) Membership.Provider; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload