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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值