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
- 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....