Hi all,
I'm new to ASP.NET and I'm trying to learn it by developing a website for my friend. Here is what I've got so far:
I used the build-in Membership provider to create user and assign roles.
I've also managed to create an admin section to manage users and their roles.
The problem I have is that each user will fall under a department category, for example user 'A' once registered will be assigned a role by the admin. User 'A' will also need to be assigned to a department.
I know that using the Profile provider the user can select the department when they register. However, I want the admin to assign them to "one or many" departments. The content displayed to user 'A' will be based first on his role and then his department.
Can anyone help me figure this out? This is similar field to role where user can be part of one to many roles.
Thanks in advance!
Regards,
Vish
Loading
Andrew FensterPosted Jul 19, 2011, 10:06 AM
I'm sorry, I don't have time to make a full code sample. Deadlines are looming.
Here's a good link about Membership: http://msdn.microsoft.com/en-us/library/ff648345.aspx
Normally, when you want to create a new user, you write code like this:
Membership.CreateUser("UserName", "P@ssw0rd", "my@emailAddress");
The Membership provider class creates a user in the Membership database. This is a rather complicated database with a lot of complicated stored procedures. So you don't want to modify any of it. Instead, you will make your own additional tables to store your own data. You can put these in the Membership database, or in your own database. Either way is fine. In your case, you might have one table to list all departments (columns: departmentID, departmentName) and another table to store department assignments (columns: username, departmentID).
Now, instead of simply calling Membership.CreateUser, you create your own class, VishanMembership. It has a method like this:
public void CreateUser(string userName, string password, string email, List departmentIDs)
Inside this method, you will
(1) Start a database transaction.
(2) Save your deparment assignments.
(3) Call Membership.CreateUser(userName, password, email). This call may fail for a number of reasons. For example, the username or email may be invalid.
(4) If Membership.CreateUser succeeded, you commit your database transaction, and you're done. If not, you roll back your database transaction and send an error message.
Now you also need to add other methods to VishanMembership to update and delete users. In each case, the method will update or delete the department assignments and also call the Membership class to update or delete the user data.
It's a lot to absorb for a new developer. Let me know if it makes sense.
Vishan S GillPosted Jul 19, 2011, 7:22 AM
Thanks for taking out some time to explain me the details. I understand the approach you are talking about. But as I'm a newbie in ASP.NET I still don't know how this can be implemented. For example, how can I make my application call MY membership class instead of the existing one? And how can I add additional information? (Will it be stored in a new table in the db?) And lastly will it allow me to add users to as many department as I want?
Can you provide me with any code sample of how this can be implemented?
Thanks & Regards,
Vish
Andrew FensterPosted Jul 18, 2011, 4:08 PM
What I've done is create my own class which contains a Membership object, plus whatever other data I want. That is, I wrap the Membership object inside another object. When someone wants to create a user, they call MY class, not the Membership class. Internally, my class calls the Membership class to create a user, and then it calls additional code to store the other user data. In your case, this class would create a new user and then save the department data.
When retrieving a user, you class would use the Membership class to get the user and then additional code to get the departments. Likewise with updates. So in essence, you're building a class which contains the Membership class plus the code to add, update and retrieve your additional data.
Good luck with it.