.NET Answers

ASP.NET, HTML, CSS, Visual Studio, CSharp, VB.NET and other programming items of interest.
Subscribe
  • Home
  • About Me
  • Advertising
  • Click Here to Ask a question
    • Privacy Policy
  • Site Map

Unique SQL Login w/ SqlMembership Provider

April 03, 2008 By: Dave

Yesterday, we looked at how to setup the web.config file and modify the business logic layer to make use of Profile properties to give us unique SQL logins when we use the SqlMembership provider for forms authentication.  Today, we are going to show you the code you will need to associate the profile properties with the user.

Most people think of profile properties as something the user associates with his own login.  What we need to do in this case is have the admin associate the properties with the login.  Fortunately, there are APIs in ASP.NET that allow us to do just that.  So, to start, I’m going to show you the update code:

Update the user information

public void UpdateLogin(string LoginName,
    string SQLUserName,string SQLPassword)
{
    ProfileCommon Profile =
      (ProfileCommon)ProfileCommon.Create(LoginName,true);
    Profile.SQLUserName = SQLUserName;
    Profile.SQLPassword = SQLPassword;
    Profile.Save();
}

Note: the Profile classes are part of the System.Web.Profile namespace.  You’ll want to include that at the top of any source file that is using it.

The first line of this code creates a profile object that is associated with the login name.  The second and third line assign the username and password.  The forth line saves that all back into the Membership database.

You may wonder where the SQLUserName and SQLPassword properties came from.  The ones that are hanging off the Profile object.  They are not part of the ProfileCommon class.  They automatically get created simply by declaring them in the web.config file.

Displaying the list of users

As I mentioned yesterday, not every user needs to have this information associated with them.  So, we need to be able to filter out the users.  In my implementation, I decided to make the SQL login association role based.  So, my code that files my DataTable that I can bind to a grid (or whatever) retrieves all of the users in the system and then adds rows to the DataTable if the user is part of that row:

public DataSetContentEditors.LoginsDataTable GetLogins()
{
    ProfileCommon Profile = new ProfileCommon();
    DataSetContentEditors.LoginsDataTable logins =
         new DataSetContentEditors.LoginsDataTable();
    MembershipUserCollection muc = Membership.GetAllUsers();
    foreach (MembershipUser mu in muc)
    {
        if (Roles.IsUserInRole(mu.UserName, "Editor"))
        {
            logins.AddLoginsRow(mu.UserName,
               Profile.GetProfile(mu.UserName).SQLUserName,
               Profile.GetProfile(mu.UserName).SQLPassword);
        }
    }
    return logins;
}

Again, I’m using the ProfileCommon class.  This time to retrieve the username and password values from the profile database.  Just like we did yesterday when we got the connection string.

The Membership.GetAllUsers() call is retrieving all of the users in the system.  My foreach statement iterates through each of those users, checks to see if they are part of the “Editor” role and if they are, add the login name, the sql username and the sql password to the LogingDataTable.

Is it Secure?

No, not out of the box.  You can visit this article at Microsoft to get more information on how to implement profile properties at various security levels:  Security Profile Properties

More Information

For links to more information see yesterday’s article.

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Related Post

  • SQL – Transactions
  • SQL – Filtering WHERE condition on two rows
  • Random in SQL – SQL For Programmers
Bookmark to:

Add to Del.icio.us Add to digg Add to DotNetKicks Add to DZone Add to Facebook Add to Slashdot Add to Stumble Upon Add to Technorati
Hide Sites
Tags: logins, sql, sqlmembership provider

Comments are closed.

← Unique SQL Logins and SqlMembership Provider
Seth is absolutely right! →
  • Search

  • Subscribe

    U COMMENT
    I FOLLOW

    Subscribe in a reader

    OR

    Subscribe via e-mail

    Enter your email address: 

    Delivered by FeedBurner

     

  • Follow Me

    • Twitter
    • FaceBook
    • Digg
    • StumbleUpon
    • Propeller
    • Delicious
    • Plaxo

     

  • Recent Posts

    • ASUS Eee PC 1005HA-PU1X-BK Black Netbook
    • jQuery – Date Picker
    • Using VB.NET From CSharp
    • iTextSharp – Adding Images
    • Hungarian Notation – Use What Works, Spit Out The Bones
    • Pre Order Windows 7
    • jQuery Dialog – With Validation Controls
    • iTextSharp – The easy way
    • Structure of my ASP.NET Web Applications
    • 35% Off Accronis True Image 2009 Home
    • VB.NET Hide Module Name
    • ASP.NET/VB.NET – Video Training
    • Does jQuery Make Us Lazy?
    • PDFs Using iTextSharp
    • Programming SEO – Ping



  • Advertise on this site through Lake Quincy Media
  • DotNetNuke Sponsor

     

    Most Valuable Blogger
  • Sponsor

  • Categories

    • Advanced CSharp
    • Advanced VB.NET
    • ASP.NET MVC
    • Did you know
    • DotNetNuke – Module Development
    • DotNetNuke – Skinning
    • internationalization
    • iTextSharp
    • jQuery
    • none
    • Seach Engine Optimization
    • Silverlight
    • SQL For Programmers
    • Twitter
    • winforms
  • Cloud

    .net ajax architecture asp.net book books containers csharp css dal dataset datasets dotnetnuke events gridview images internationalization internet explorer javascript jQuery json linq listview modules ms-sql MVC objectdatasource programming reflection seo Silverlight skinning sql testing tsql tutorial Twitter twitterizer vb.net video view Vista visual studio webservice WordPress
  • Archives

    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
    • Privacy Policy
  • Calendar

    April 2008
    S M T W T F S
    « Mar   May »
     12345
    6789101112
    13141516171819
    20212223242526
    27282930  
  • Blogroll

    • Alvin Ashcraft’s Morning Dew
    • ASP.NET Consulting
    • Life Hacker
    • Remember Anything
    • The Price of Their Toys
    • Uncategorized Thought


.NET Answers © 2007 - 2008 All Rights Reserved.
Entries and Comments.