在你的配置中,如何实现自定义 configSection

分享于 

5分钟阅读

Web开发

  繁體 雙語

介绍

这里,我将提供有关如何在应用程序中实现自定义 configSection的详细信息。

假设你想为自定义成员实现创建一个 configSection,如下所示:

<memberships><memberusername="test"password="testpassword"role="client"/><memberusername="hostuser"password="private"role="client,host"/></memberships>

为此,你需要创建如下所需的类。

MembershipElement.cs
publicclass MembershipElement : ConfigurationElement
{
 [ConfigurationProperty("username", IsRequired = true, IsKey = true)]
 publicstring Username
 {
 get { return base["username"] asstring; }
 set { base["username"] = value; }
 }
 [ConfigurationProperty("password", IsRequired = true, IsKey = false)]
 publicstring Password
 {
 get { return base["password"] asstring; }
 set { base["password"] = value; }
 }
 [ConfigurationProperty("role")]
 publicstring Role
 {
 get { return base["role"] asstring; }
 set { base["role"] = value; }
 }
}
MembershipCollection.cs
publicclass MembershipCollection : ConfigurationElementCollection
{
 protectedoverride ConfigurationElement CreateNewElement()
 {
 returnnew MembershipElement();
 }
 protectedoverrideobject GetElementKey(ConfigurationElement element)
 {
 return ((MembershipElement)element).Username;
 }
 protectedoverridestring ElementName
 {
 get {
 return"member";
 }
 }
 protectedoverridebool IsElementName(string elementName)
 {
 return!String.IsNullOrEmpty(elementName) && elementName == "member";
 }
 publicoverride ConfigurationElementCollectionType CollectionType
 {
 get {
 return ConfigurationElementCollectionType.BasicMap;
 }
 }
 public MembershipElement this[int index]
 {
 get {
 returnbase.BaseGet(index) as MembershipElement;
 }
 }
 public MembershipElement this[string key]
 {
 get {
 returnbase.BaseGet(key) as MembershipElement;
 }
 }
}
MembershipConfigurationHandler.cs
publicclass MembershipConfigurationHandler : ConfigurationSection
{
 [ConfigurationProperty("", IsDefaultCollection = true, IsKey = false, IsRequired = true)]
 public MembershipCollection Members
 {
 get {
 return base[""] as MembershipCollection;
 }
 set {
 base[""] = value;
 }
 }
}

现在你已经准备好在应用程序中使用定制的configSection 了。 应用程序配置插件文件的将如下所示:

<configSections><sectionname="memberships"type="Authentication.Configuration.MembershipConfigurationHandler, Authentication"/></configSections>
<memberships><memberusername="test"password="testpassword"role="client"/><memberusername="hostuser"password="private"role="client,host"/></memberships>

我创建了一个 helper 类,以便轻松访问成员详细信息。

MembershipProvider.cs
publicstaticclass MembershipProvider
{
 publicstatic MembershipElement GetMember(string userName)
 {
 var memberships = ConfigurationManager.GetSection("memberships") as MembershipConfigurationHandler;
 if(memberships == null || memberships.Members == null || memberships.Members.Count <1)
 returnnull;
 return memberships.Members[userName];
 }
 publicstatic MembershipElement ValidateUser(string userName, string password)
 {
 var member = GetMember(userName);
 if (member!= null && member.Password.Equals(password))
 return member;
 returnnull;
 }
}

你可以使用这里提供程序来验证成员或者获取成员详细信息。 在应用程序中使用这里提供程序来验证成员并在当前线程中设置角色。

CustomUsernamePasswordValidator.cs
publicclass CustomUsernamePasswordValidator : UserNamePasswordValidator
{
 publicoverridevoid Validate(string userName, string password)
 {
 var member = MembershipProvider.ValidateUser(userName, password);
 if(member == null)
 thrownew FaultException<SecurityTokenException>
 (new SecurityTokenException("Authentication failed"),
 "Unknown Username or Password");
 Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(userName),
 member.Role.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
 }
}

我在WCF服务应用程序中使用这里验证程序来验证使用自定义用户名和密码。 我将在其他文章中解释。

感谢你的查询和建议。