在Windows Forms中使用Membership provider ------[轉]

轉載自:http://www.cnblogs.com/sunrack/articles/Membership.html

 

Although the membership API was originally built for ASP.NET and web applications, you can also
use it for Windows Forms–based applications. Indeed, the Microsoft patterns & practices group is
doing that in the latest version of the Microsoft Enterprise Library, version 3.x, for the .NET Framework
in the authentication and authorization building block as well.
All you need to do to use membership in your Windows Forms applications is follow these steps:
1. Add a reference to System.Web.dll.
2. Create a database using aspnet_regsql.exe.
3. Add an app.config application configuration file.
4. Add a connection string to your app.config file that points to the membership database.
5. Add a <system.web> configuration section to your app.config file.
6. Configure the <membership> section within <system.web> in your app.config file.
7. Run the application, and access the database through the membership API classes.
With this infrastructure in place, you can implement security the same way in Windows Forms
as you do in your web applications. In addition, this opens up possibilities for creating common
components that you can use for both ASP.NET and Windows Forms applications. Suppose you
have a Windows Forms application as demonstrated in Figure 21-22. It allows you to add users to
your database, and it displays users in the database in a simple ListView control.

 

This application needs to have an application configuration file similar to the following:
<configuration>
<connectionStrings>
<add name="WinConnString"
connectionString="..."/>
</connectionStrings>
<system.web>
<membership defaultProvider="WinConnProvider">
<providers>
<add name="WinConnProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="WinConnString"
applicationName="testapp"
requiresQuestionAndAnswer="false" />

 

</providers>
</membership>
</system.web>
</configuration>
With this configuration in place, add a reference to System.Web.dll in your project, and import
the System.Web and System.Web.Security namespaces (although this looks a little bit strange in a
Windows Forms application). The following code then works without any problems:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MembershipUserCollection users = Membership.GetAllUsers();
foreach (MembershipUser user in users)
{
ListViewItem lvi = new ListViewItem();
lvi.Text = user.UserName;
lvi.SubItems.Add(user.Email);
lvi.SubItems.Add(user.CreationDate.ToString());
UsersListView.Items.Add(lvi);
}
}
private void AddCommand_Click(object sender, EventArgs e)
{
try
{
Membership.CreateUser(UserNameText.Text,
PasswordText.Text,
EmailText.Text);
MessageBox.Show("User created!");
}
catch (Exception ex)
{
MessageBox.Show("Unable to create user: " + ex.Message);
}
}
}
This is a nice way to create Windows Forms applications using the ready-to-use user-management
framework provided by the membership and roles APIs. As mentioned, Microsoft is doing the same with
its latest version of the Microsoft Enterprise Library for .NET from the patterns & practices group (see
http://msdn.microsoft.com/patterns).

發佈了38 篇原創文章 · 獲贊 5 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章