Assign Default Role to New User in ASP.NET

25. January 2010

Recently I was working on a project to help administer the user accounts that used the asp.net membership provider.  Why was I doing this? I did this because you can't really use the ASP.NET Configuration Tool in Visual Studio to manage a remote website. Now I'm sure there is a way to take the files that it uses and make it work on a remote server, but probably more time than what it's worth. 

Now, over my years of coding I personally have not used the asp.net membership provider (weird huh) because I either used Active Directory for internal applications, or made my own solution for managing user accounts.  I will admit, I do like the asp.net membership provider and what it has to offer and I will definitely consider it for future solutions. The one thing I did find myself doing was making custom templates for managing the accounts. I didn't mind doing this because the classes and sql side were already setup for me to pass my values into it (thanks sql membership provider!). 

I'll finally get to the point of this blog post. I was a bit dissapointed with no "default way" to add a user to a role when creating a new user. No big deal though, wasn't too hard to write the code for it.  So this is how I went about automatically adding a new user to a role.

Prerequisite: Must have the role created before trying to add the user to the role

ASPX PAGE

Code Behind

And that is that. Using the built in provider makes is super easy to accomplish this task.  Here is a great reference link for those just starting out using the ASP.NET Membership Provider:

http://www.4guysfromrolla.com/articles/120705-1.aspx

Side note: I think the only problem I had with the link above is the tool they use to manage directory access, the app would need read/write access to write the XML file to manage that.  I chose to stay away from that example because if you host it on a shared server with someone like Go Daddy, you would need to give write access to the directories you would want to secure.

Good luck.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

ASP.NET

blog comments powered by Disqus