 |
BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9
 | This is the forum to discuss the Wrox book Professional ASP.NET 3.5: In C# and VB by Bill Evjen, Scott Hanselman, Devin Rader; ISBN: 9780470187579 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

May 7th, 2009, 03:32 PM
|
|
Authorized User
|
|
Join Date: Dec 2008
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Programmatically Assigning Roles to Users?
Hello,
I realize this may be in the wrong forum, but since I'd posted a couple of questions from the Wrox book, I wanted to ask a follow-up question.
The book talked about how to assign roles to users via the Security tab on the ASP.NET Web App Admin pages.
My question is how to programmatically assign (or revoke) a role to the current user. We have several interactive apps we want to upgrade to .NET (from ASP Classic), and roles will play a big part.
For example, we have an online course registration page. Under the new setup, the logged-in user automatically acquires the "Participant" role as soon as he successfully completes the registration page. I do not want to have to assign this role manually to new participants via the Manage Users page; I'd never be able to keep up. None of my co-workers would have access to that area, so I'd be the sole controller.
This only comes into play the first time he ever registers for any of our courses; after that, he will always retain that role. I don't foresee any scenario at this time where he would lose that role.
Returning course participants will already have the "Participant" role because of their previous registrations, so nothing new needs to happen for them role-wise.
Any pointers or tips on how this can be accomplished? I'm sure there's a way and maybe either I missed it while reading the book, or maybe it wasn't covered. I do recall a C# code snippet using the IsInRole keyword, but that was a testing statement; I don't recall seeing anything that gives the user a certain role.
Thanks!!!!
__________________
~ Eddie McHam
|
|

May 7th, 2009, 04:44 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
You can use the Membership and Roles APIs. For a simple example, take a look here:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=472
The article discusses just some basic operations on these APIs but I think they'll give you enough to dig a little deeper yourself. E.g when you know how to use CreateRole, finding DeleteRole is pretty easy.
Please let me know if you need more information.
Cheers,
Imar
|
|

June 2nd, 2009, 05:58 PM
|
|
Authorized User
|
|
Join Date: Dec 2008
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
It's me again.
Cannot seem to get this SQLMembershipProvider database going. I've been battling it out with WSAT's Security tab pretty much all day, and all I get is the following:
Quote:
There is a problem with your selected data store. This can be caused by an invalid server name or credentials, or by insufficient permission. It can also be caused by the role manager feature not being enabled. Click the button below to be redirected to a page where you can choose a new data store.
The following message may help in diagnosing the problem: Unable to connect to SQL Server database.
|
I do have roleManager set to True in web.config, and I did have to re-install my SQLEXPRESS, since it apparently didn't install right the first time. (WHY Microsoft can't make this a simpler process is beyond me, but I digress.) Finally, I checked my App_Data folder permissions, and I cannot seem to get it to stop applying Read-Only. I am wondering if this is the source of my problems.
One other peculiar aspect of my project....its files are temporarily residing on a USB flash drive until my work computer is repaired and back in my office. (Hard drive cratered last week, and I'm on a loaner PC in the meantime.) I don't know if this affects things or not.
Net effect: I am stuck on this WSAT thing, and I couldn't find the ASP.NET SQL Server Registration tool anywhere. Is it only included with VS, or does VWD come with it, too?
I sure hope this isn't an indicator of how the rest of my project's gonna be.......
Thanks, Eddie
__________________
~ Eddie McHam
|
|

June 4th, 2009, 03:52 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I am not 100% sure, but I think you can only use / attach database files that are on the local file system. Try connecting it to SQL Server using Management Studio and see if that works.
Cheers,
Imar
|
|

June 4th, 2009, 11:27 AM
|
|
Authorized User
|
|
Join Date: Dec 2008
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
It appears the SQLXPRESS database is already attached to the project. Trouble is, VWD doesn't see the aspnet tables, whereas SQL Mgmt Studio does. So if VWD can't see them, then I can't create the membership.
With all the trouble I had installing SQLEXPRESS the first time around, I'm starting to think there's something deeper going on. So I'm going to uninstall and reinstall both VWD and SQL Mgmt Studio and restart this whole .NET project from scratch. I don't have that many ASPX files created just yet, and the ones I do have, I can back up to a temp folder and re-import into a brand new project as needed, so I shouldn't have to re-invent too many wheels.
I have been following the Planet Wrox examples in the book as a rough guide to building our project, since I see some resemblance between the two. I am hoping either I've just missed something the first time around, or maybe a cleaner SQL/VWD installation will work out whatever is holding me up. I've been using the 2008 versions of both, and they seem to look and act much like the 2005 versions cited in the book, from what I can tell.
Regards, Eddie
__________________
~ Eddie McHam
|
|

June 5th, 2009, 02:40 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
It appears the SQLXPRESS database is already attached to the project.
|
Also for the PlanetWrox site? Normally, you either attach the database to SQL Server and then use Management Studio (or VWD) to manage it or you just add your database to App_Data and manage it with VWD.
The databases used by Membership and Roles are created on the fly, in App_Data by default.and are called aspnetdb.mdf. If you want to change that, you need to use the reg tool you talked about (aspnet_regsql) which ships with the .NET Framework (so you don't need VS or VWD for it). You then need to configure your application to use this new database.
Since I don't know your setup, I cannot really recommend much. Maybe you can elobarate a bit on the steps you have taken so far and the things you're trying to accomplish?
Imar
|
|

June 5th, 2009, 07:48 PM
|
|
Authorized User
|
|
Join Date: Dec 2008
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
Sorry, I should clarify. I am using the Planet Wrox examples in the book to set up our project, which I've named HNTC.NET. No actual files or DBs from the Planet Wrox project are involved here.
Well, I must say honestly this has been three of the most nerve-wracking days I have ever spent on a new project. Not your fault  ~ I just could not get that screwball ASPNETDB up and running to where WSAT would let me manage users the way your book and countless Google searched articles all said it should. So I'm sure the problem was somewhere either in my setup or in how VWD and/or SQL MS 2008 installed.
I finally got so frustrated yesterday that today I completely uninstalled and reinstalled VWD and SQL Mgmt Studio Express -- both 2008, the latter I suspect being the main culprit.
After reinstalling both apps, I went back through the Planet Wrox exercises. This time, I did finally get ASPNETDB.MDF to generate and WSAT to let me in the Security tab. However, VWD would not let me dbl-click on ASPNETDB.MDF in either Solution or DB Explorer. I kept getting this error msg about SQL Server Express 2005 being required, even though SQL Server Express 2008 was installed as part of your procedure for installing WVD optional items.
So I reinstalled SQL Mgmt Studio Express 2008 (SMSE). Although it saw SQLEXPRESS, it insisted on setting up a totally new instance, whose name I do not know. I then tried attaching ASPNETDB.MDF to SMSE, but got another error msg about not having the right permissions. So I had to give this great big long SQL user account read/write permissions to the folder in which ASPNETDB.MDF sat. Finally, SMSE let me attach ASPNETDB.MDF, and I could finally see the encrypted user record I'd created for myself via my Login.aspx page.
But then VWD stopped letting me anywhere near ASPNETDB.MDF, and WSAT quit working again ~ something about user permissions. Rebooted PC, just on a whim, and I'm back in WSAT Security tab again. At this point, I am afraid to go near SMSE again, as that always seems to be where my problems start.
I am quite literally praying to God both apps will behave themselves come Monday morning and ever after.
I did want to let you know of my progress and that I am very open to any other input you may want to send my way. I also appreciate very much your book and your helping a complete n00b like me out here.
It is Friday and well past my quitting time; I am SO homeward bound. Hope you enjoy your weekend, too.
Kindest regards,
Eddie
__________________
~ Eddie McHam
|
|

June 7th, 2009, 05:51 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
Although it saw SQLEXPRESS, it insisted on setting up a totally new instance, whose name I do not know.
|
That seems to imply you still had an old version running that wasn't uninstalled completely. If you go into Control Panel | Administrative Tools | Services do you see SQL Server there somewhere? Also, try the "SQL Server Configuration Manager" tool which should be somehwere in your SQL Server start menu.
Quote:
|
I then tried attaching ASPNETDB.MDF to SMSE, but got another error msg about not having the right permissions. So I had to give this great big long SQL user account read/write permissions to the folder in which ASPNETDB.MDF sat.
|
Normally, when you set up SQL Server you can choose that account during setup and the installer takes care of it. You typically use the Network Service account. Didn't you get that option during installation?
Quote:
Finally, SMSE let me attach ASPNETDB.MDF, and I could finally see the encrypted user record I'd created for myself via my Login.aspx page.
But then VWD stopped letting me anywhere near ASPNETDB.MDF, and WSAT quit working again ~ something about user permissions.
|
Did you reconfigure your application afterward? If you attach a database in SMSE you need to tell your application where to find that database; otherwise it will still try to create and attach a new aspnetdb on the fly. Take another look at this article:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=472
It shows the web.config settings you need to make this work. Notice how each setting refers to a connection string called LocalSqlServer. This is the default, but you can replace it with your own. Then add that connection string under <connectionStrings /> in the same config file and make sure it points to your attached database. For more info, these articles may come in handy as well:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=395
http://weblogs.asp.net/scottgu/archi...25/423703.aspx
Also, for proper connection strings: www.connectionstrings.com
And yeah, my weekend is fine so far. Thanks... ;-)
Cheers,
Imar
|
|

June 8th, 2009, 11:47 AM
|
|
Authorized User
|
|
Join Date: Dec 2008
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
I think I have it now.
I got kind of mixed up on the part about "LocalSqlServer." I copied and pasted your snippet and kept getting error messages about "LocalSqlServer" already being added.
Looking through another article, I suddenly realized that "LocalSqlServer" is a special keyword referring to the App_Data/ASPNETDB.MDF file. Apparently I failed to make that mental connection when I read your last post.
So I renamed my connectionString "SqlXpress" and changed the other references accordingly. Now everything seems to be working ~ WSAT Security tab lets me manage users in the new DB; VWD DB Explorer (at long last!) lets me view records w/o the need for SMSE, and my login and create user ASPX web forms appear to be functioning properly.
Now that VWD's DB Explorer lets me view records from .\SQLEXPRESS\ASPNETDB, I assume this means I no longer have any real need for the App_Data/ASPNETDB.MDF or SMSE. In other words, I should be able to just call .\SQLEXPRESS\ASPNETDB directly from any login-related control on my ASPX pages.....correct?
Assuming I do indeed have all this working, I can proceed with adding some users, creating and assigning new roles, and creating a custom profile form for our staff and website visitors to use.
When I'm ready to upload everything to our production servers, I can simply go to DB Explorer, right-click the new ASPNETDB, and choose Publish to Provider, which should generate a SQL script I can run on our production SQL servers.
After that....I guess this means I'll have to fiddle with my SQL connection settings in web.config again, right?
Thanks again so much!
~Eddie
__________________
~ Eddie McHam
|
|

June 8th, 2009, 02:22 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
I suddenly realized that "LocalSqlServer" is a special keyword referring to the App_Data/ASPNETDB.MDF
|
The LocalSqlServer is defined in the machine-wide machine.config and serves as the default. Its connection string points to .\SqlExpress.
You can use <clear /> as the first element in connectionStrings to get rid of existing ones so you can readd LocalSqlServer if you wish.
Quote:
|
I should be able to just call .\SQLEXPRESS\ASPNETDB directly from any login-related control on my ASPX pages.....correct?
|
You don't have to "point" to anything. If the web.config is setup OK, and Membership, Roles etc have a connection string setting that points to a connection string that works, everything is taken care of automatically.
Quote:
|
When I'm ready to upload everything to our production servers, I can simply go to DB Explorer, right-click the new ASPNETDB, and choose Publish to Provider, which should generate a SQL script I can run on our production SQL servers.
|
Yes; that'll create a SQL script you can use to recreate the database with your host.
Quote:
|
I guess this means I'll have to fiddle with my SQL connection settings in web.config again
|
Yep, correct. However, since you've already defined the Membership and Roles items all you need to do is change the connection string. Your host will be able to tell you what you need.
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|
 |