企业库2.0安全应用程序块使用(1)
企业库2.0安全应用程序块使用(1)
第一次使用企业库,不当之处,请高手指点。
一、添加对Caching Common Security objectBuilder四个dll的引用
二、完整代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Security;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Security;
using System.Security.Principal;
using System.Web.Security;
using System.Configuration.Provider;
namespace SecurityApp
{
public partial class Form1 : Form
{
ISecurityCacheProvider cache;
IIdentity identity = null;
IToken token = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//cache = SecurityCacheFactory.GetSecurityCacheProvider("Caching Store Provider");
//if (Membership.ValidateUser("Username", "password"))
//{
// identity = new GenericIdentity("username", Membership.Provider.Name);
//}
//if (identity != null)
//{
// token = cache.SaveIdentity(identity);
//}
IPrincipal principal = new GenericPrincipal(new GenericIdentity("uame"), new string[] { "Manager" });
IAuthorizationProvider ruleProvider = AuthorizationFactory.GetAuthorizationProvider("RuleProvider");
// Determine whether user is authorized for the rule defined as "Print Document".
bool authorized = ruleProvider.Authorize(principal, "FullAccessToDatabase");
if (authorized)
MessageBox.Show("UserName Have the full access to database");
else
MessageBox.Show("UserName have no access to database");
}
}
}
三、配置文件中
添加安全应用程序块,在RuleProvider上新建一个规则,明明为FullAccessToDatabase,其value为I: Username。
如果程序中的GenericIdentity的参数为Username,则通过认证。否则 程序输出UserName have no access to database。
浙公网安备 33010602011771号