Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

How to get user roles and automatic user creation working?

$
0
0

I've always found it so hard to get roles and authentication finally working! And now the ASP Configuration console was removed, to make things even harder.

I have an already working ASP.NET web app with C# and .NET framework 4. It was built in VS 2012, but now I'm using VS2013.

This is what I've done so far, and it's not working correctly:

1. In Web.config, inside the <system.web> tag I inserted this:

   <roleManager enabled="true" defaultProvider="simple">

     <providers>

       <clear />

       <add name="simple" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />

     </providers>

   </roleManager>

   <membership defaultProvider="simple">

     <providers>

       <clear />

       <add name="simple" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />

     </providers>

   </membership>

2. In Global.asax, I added the [InitializeSimpleMembership] annotation on top of the following method: protected void Application_Start()

I also moved [InitializeSimpleMembership] from the AccountController to the HomeController (so now I have two [InitializeSimpleMembership] annotations).

3. In my seed method (that receives the context class as a parameter) I call a "SeedMembership" method like this:

privatevoid SeedMembership()

{

      WebSecurity.InitializeDatabaseConnection("DefaultConnection","UserProfile","UserId","UserName", autoCreateTables:true);

      var roles = (SimpleRoleProvider)Roles.Provider;

      var membership = (SimpleMembershipProvider)Membership.Provider;

 

      if (!roles.RoleExists("rol1"))

      {

            roles.CreateRole("rol1");

      }

 

      if (!roles.RoleExists("rol2"))

      {

            roles.CreateRole("rol2");

      }

 

      if (membership.GetUser("user1",false) ==null)

      {

            membership.CreateUserAndAccount("user1","password");

      }

      if (!roles.GetRolesForUser("user1").Contains("rol1"))

      {

            roles.AddUsersToRoles(new[] { "user1" },new[] {"rol1" });

       }

}


The database seems to be working, as the rest of the app works. Just in case, this is my database connection:

 <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-consultorio;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-consultorio.mdf" providerName="System.Data.SqlClient" />
  </connectionStrings>

I'm not sure what's wrong, but "user1" is definitely not being created, as I get a user/pass error when trying to log in.

I created another user manually (this one works when logging in) and tried adding that user to "rol1" (with the code in the Seed method) and then using [Authorize(Roles = "rol1")] on a specific controller method to test it, but whenever I click on the link that calls that method I get a login screen, so it's not even adding my manually created user to the role I specified.

Any help will be appreciated!


Viewing all articles
Browse latest Browse all 4737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>