<June 2019>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

On this page...

Search

Links

Member of...


ASP Insiders

MVP Visual Developer ASP/ASP.NET

Enter CodeZone

Blog Categories

Microsoft

Blogroll

Deutsche Resourcen

Management

Sign In
 

#  Thursday, July 28, 2005

Did you know that the Membership API isn't limited to being used inside ASP.NET 2.0 applications? That you can also use it in say a Console application? This is actually very useful, because that spells automated administration for me.

In order to demo this, I set out and created a very simple Web application that lists users registered with the Membership system:

This simply calls Membership.GetAllUsers() and we are set. More interesting is web.config (partial view):

  <connectionStrings>
    <add name="NWConn"
connectionString="Data Source=cbc05vpc\cbc05;Initial Catalog=Northwind;User=sa;Password=P@ssw0rd"/>
  </connectionStrings>
  <system.web>
    <membership defaultProvider="myMembership">
      <providers>
        <clear/>
        <add name="myMembership"
          applicationName="DemoApp"
          connectionStringName="NWConn"
          type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, ..." />
      </providers>
    </membership>

This is using the stock SqlMembershipProvider, and stores data inside the Northwind database. Pretty vanilla, except for one important attribute: applicationName. Because one Membership database can hold accounts for multiple applications, we need to define the name here unless we want to end up with guessing that name for the console application.

Now let's switch to the Console application. It obviously needs to reference System.Web.dll:

Secondly, it needs an App.Config file (this time, in full glory):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add
      name="NWConn" connectionString="Data Source=cbc05vpc\cbc05;Initial Catalog=Northwind;User=sa;Password=P@ssw0rd"/>
  </connectionStrings>
  <system.web>
    <membership defaultProvider="myMembership">
      <providers>
        <clear/>
        <add name="myMembership"
           applicationName="DemoApp"
           connectionStringName="NWConn"
           type="System.Web.Security.SqlMembershipProvider, System.Web, ..." />
      </providers>
    </membership>
  </system.web>
</configuration>

As you can see, I simply copied its contents verbatim from the previously shown web.config. All we now need is to access the Membership API inside our application:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;

namespace TheConsoleApp
{
 class Program
 {
   static void Main(string[] args)
   {
     MembershipUserCollection muc = Membership.GetAllUsers();
     foreach (MembershipUser mu in muc)
      Console.WriteLine(mu.UserName + " " + mu.Email);
   }
 }
}

Surprise, surprise - it works as expected:

Done.

MembershipEverywhere.zip (19.28 KB)

Categories: 2 Ohhhh | ASP.NET
Thursday, July 28, 2005 2:27:21 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [2]

 



Wednesday, May 3, 2006 2:10:39 PM (W. Europe Daylight Time, UTC+02:00)
Is it possible to use Profiles in Windows Forms ?
Arno Nel
Wednesday, May 3, 2006 3:06:16 PM (W. Europe Daylight Time, UTC+02:00)
Membership can be used in Windows Applications, however, I do recommend that you take a look at the Enterprise Library because it is geared towards that.
Chris
Comments are closed.

© Copyright 2019 Christoph Wille

newtelligence dasBlog 2.3.9074.18820
Subscribe to this weblog's RSS feed with SharpReader, Radio Userland, NewsGator or any other aggregator listening on port 5335 by clicking this button.   RSS 2.0|Atom 1.0  Send mail to the author(s)

 
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.