TECLAHOST - FORUM ABOUT HOSTING, HOUSING, VIRTUAL SERVER, DEDICATED SERVER, E-MAIL
Use Membership API in Winforms  
Home Help Search Login Register
 

Username  
Password
Pages: [1]   Go Down
  Print  
Author Topic: Use Membership API in Winforms  (Read 900 times)
kilop
Membro Sénior
****
Offline Offline

Posts: 114


View Profile
« on: August 05, 2009, 06:19:52 pm »

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 

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.
Create a New Windows Application

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....
Logged
kilop
Membro Sénior
****
Offline Offline

Posts: 114


View Profile
« Reply #1 on: August 05, 2009, 06:21:33 pm »

Please, follow this link :

Unify Windows Forms and ASP.NET Providers for Credentials Management

http://msdn.microsoft.com/en-us/magazine/cc163807.aspx
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  




MySQL |  PHP |  Valid XHTML |  Valid CSS