# 副標=安全管制新功能(1)
#大標= ASP.NET 2.0網頁安全管理
#眉標= ASP.NET 2.0
#作者=文/王寧疆




=============box 程式1===============
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <system.web>
    <authentication mode="Forms">
      <forms name="name" 
	cookieless="UseCookie|UseUri|AutoDetect|UseDeviceProfile" 
	defaultUrl=""[URL=""] 
	domain="domain" name=""
	loginUrl="url" 
	protection="All|None|Encryption|Validation"
	timeout="30"  
	path="/"
	requireSSL="true|false"
	slidingExpiration="true|false">
	<credentials passwordFormat="Clear|SHA1|MD5">
	 <user name="username" password="password"/>
	</credentials>
      </forms>
    </authentication>
  </system.web>
</configuration>

================= end =================




=============box 程式2=============
<authentication mode="Forms">
	<forms name="name" loginUrl="url" protection="All" timeout="30" 
		path="/" requireSSL="true" slidingExpiration="true">
		<credentials passwordFormat="SHA1"/>
	</forms>
</authentication>

=================end==================




=============Box 程式3============
<identity impersonate="true" 
	userName="registry:HKLM\software\WebApplication1\Identity\_SPNET_SETREG,userName" 
	password="registry:HKLM\software\WebApplication1\Identity\_SPNET_SETREG,password" />
=================end=============




===================box 標籤1===============
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ 
     ConnectionStrings:NorthwindConnectionString %>"
      SelectCommand="SELECT * FROM [Suppliers]">
</asp:SqlDataSource>
====================== end =====================





====================== box 程式4 ===================
<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=.;Initial 
 Catalog=Northwind;User ID=sa"
            providerName="System.Data.SqlClient" />
</connectionStrings>
======================== end ======================




====================== box 程式5 ================
Configuration config = WebConfigurationManager.OpenWebConfiguration(
Request.ApplicationPath);	
//取得web.config設定檔
ConfigurationSection section = 
     config.Sections["connectionStrings"];	
     //取得ConnectionStrings區段
section.SectionInformation.ProtectSection(
     "DataProtectionConfigurationProvider");
     //使用DPAPI加密功能對ConnectionStrings區段執行加密
config.Save();	
	//將加密後的結果存回web.config設定檔
======================== end ===================




=============== box 程式6 =================
<protectedData>
    <protectedDataSections>
        <add name="connectionStrings" provider="DataProtectionConfigurationProvider"
                inheritedByChildren="false" />
    </protectedDataSections>
</protectedData>
================= end ====================




================= box 程式7 ==================
<connectionStrings>
    <EncryptedData>
        <CipherData>
            <CipherValue>AQAAAN以下省略</CipherValue>
        </CipherData>
    </EncryptedData>
</connectionStrings>
==================== end =====================




================= BOX 程式8 ====================
Configuration config = 
     WebConfigurationManager.OpenWebConfiguration(
     Request.ApplicationPath);	//取得web.config設定檔
config.ConnectionStrings.ConnectionStrings.Add(new 
ConnectionStringSettings("pubsConnectionString", "server=.;database=pubs;integrated security=true"));			//新增資料到加密區段
config.Save();	//將加密後的結果存回web.config設定檔
======================= end ========================




================== box 程式9 ==============
Configuration config = 
     WebConfigurationManager.OpenWebConfiguration(
     Request.ApplicationPath);	//取得web.config設定檔
ConfigurationSection section = 
     config.Sections["connectionStrings"];	 //取得被加密的區段名稱
section.SectionInformation.UnprotectSection(); //對區段的內容解密
config.Save();	//將解密後的結果存回web.config設定檔
===================== end =================




=================== box 程式10 =================
<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=.;Initial 
 Catalog=Northwind;User ID=sa"
            providerName="System.Data.SqlClient" />
    <add name="pubsConnectionString" connectionString="server=.;database=pubs;integrated 
     security=true" />
</connectionStrings>
==================== end =========================




================== box 程式11 ==================
string[] UserNames = 
     txtUserName.Text.Split(' ');
     //取出在txtUserName欄位中輸入的以空格分隔的多個使用者名稱
Roles.AddUsersToRole(UserNames, DropDownList1.Text);
	//將取得的使用者名稱加入到指定的角色中
======================= end ====================




==================== box 程式12 ====================
string[] UserNames = Roles.GetUsersInRole("Manager");
	//取出名稱為Manager的角色的所有成員
foreach (string s in UserNames)	//取出所有的成員名稱
{
 lbManager.Items.Add(s);	//將取出的成員名稱加入到ListBox控制項中
}
======================= end ========================





====================== box 程式碼13 ======================
if (User.IsInRole("Manager"))	
//判斷網頁使用者的角色是否是名稱為Manager角色的成員
{
  btnAdd.Enabled = true; //啟用btnAdd按鍵
}
else
{
  btnAdd.Enabled = false; //禁用btnAdd按鍵
}
============================== end ========================