Humprey Evangelista Cogay

My Programming Adventures

Use Membership API in Winforms

I usually code my own authentication module in my Windows Application but since i have been playing with Web Applications lately, I thought, maybe there's a way  to use the Membership API inside System.Web on my Windows Applications. So after playing and Googling around i have made it worked.

Below are the procedures  

  1. Use aspnet_regsql.exe (can be found inside C:\WINNT\Microsoft.NET\Framework\v2.0.50727 for VB 2005 Express ) to create your Membership related tables and Stored Procedures, Just follow the on screen procedures, I have tested this with SQL Server 2000.
  2. Create a New Windows Application
  3. Open your App.Config and add the following entries
    <connectionStrings>
        <add name="MySqlConnection"
             connectionString="Server=YOURSERVERNAME; Database=YOURDATABASE; User Id=YOURUSERID; password=YOURPASSWORD"
             providerName="System.Data.SqlClient" />
    </connectionStrings
<system.web>
        <membership defaultProvider="SqlProvider">

            <providers>

                <clear />

                <add name="SqlProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="MySqlConnection"

                   applicationName="MYAPPLICATIONNAME"

                   enablePasswordRetrieval="false"

                   enablePasswordReset="false"

                   requiresQuestionAndAnswer="false"

                   requiresUniqueEmail="false"
                         
               passwordFormat="Hashed" />

            </providers>

        </membership>
    </system.web>
 

                4. Add a reference to System.Web

                5. add an Imports System.Web.Security statement on top of your main form code

                6. To create a new user just use the sample code below

                           Try
                                        Membership.CreateUser("Administrator", "p@ssword1")
                            Catch ex As MembershipCreateUserException
                                        MsgBox(ex.Message)
                            End Try

               7. To authenticate a user use the sample code below

 

                            If Membership.ValidateUser(txtUserId.Text, txtPassword.Text) = True Then
                                    frmMain.Show()                                  
                            Else
                                    ShowError("Invalid Password or UserName")
                            End If  
 

 And thats all....

 Hope this one helps.

 

Sorry if the Code is a little Bit Messy still trying to figure out the best way of pasting Codes....
 

Comments

raseryu said:

Cool...

# August 22, 2007 12:50 AM

Dan said:

Very usefull - thanks

# January 9, 2008 9:44 AM

Sebastien said:

I have a question for you, in :

"connectionString="Server=YOURSERVERNAME"

does the YOURSERVERNAME can be the URL of a website?

I mean, I have a client application in VB.NET, and a website which use authentication. I would like the user to register in the application, and push the username/password/name on the website registration database.

Is it possible with that code?

Thanks

# April 16, 2008 7:52 AM

Comgen said:

oh Sebastien, Sorry for the late reply.

I havent tried this out, but i think if your SQL Server allows access from outside your domain. I think there will be no problem...

# April 23, 2008 12:01 AM

Comgen said:

oh Sebastien, Sorry for the late reply.

I havent tried this out, but i think if your SQL Server allows access from outside your domain. I think there will be no problem...

# April 23, 2008 12:01 AM

K.C said:

Hi,

it works fine for me ,tthank you very much for your help.

many thanks,

K.C

# June 18, 2008 5:57 AM

Sarika said:

Very Userful..not tried but after reading hope it will work for me :)

# July 1, 2008 9:04 AM

Alex said:

Works like a charm, thank you very much!!!

# September 25, 2008 11:49 AM

Humprey Evangelista Cogay said:

I have coded several Windows Applications using .Net for the bank for the past years and one of the ussual

# December 10, 2008 10:34 PM

tn said:

What will happen if the client open the app.config using notepad? he/she can clearly see your connection string.

# December 11, 2008 1:10 AM

Unify Windows Forms and ASP.NET Providers for Credentials Management | Tech Trend Watching said:

Pingback from  Unify Windows Forms and ASP.NET Providers for Credentials Management | Tech Trend Watching

# December 12, 2008 6:35 PM

Comgen said:

hello TN its been months hehehe.... Just for the sake of others reading this entry, You can encrypt your Connection strings in the app.config....

# May 25, 2009 6:39 PM