<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Horn Network &#187; ASP.NET</title>
	<atom:link href="http://klcin.tw/net/category/net/aspnet-net/feed" rel="self" type="application/rss+xml" />
	<link>http://klcin.tw/net</link>
	<description>Horn Network (.NET, ASP.NET, C#, VB.NET, JavaScript, Ubuntu, Android ...)</description>
	<lastBuildDate>Fri, 04 Mar 2011 07:25:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>在 WebService 用 SoapHeader 傳遞認證資訊並用 DESCryptoServiceProvider 加解密</title>
		<link>http://klcin.tw/net/webservice-soapheader-descryptoserviceprovider</link>
		<comments>http://klcin.tw/net/webservice-soapheader-descryptoserviceprovider#comments</comments>
		<pubDate>Thu, 02 Apr 2009 08:21:40 +0000</pubDate>
		<dc:creator>klcintw</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Web Service]]></category>
		<category><![CDATA[DESCryptoServiceProvider]]></category>
		<category><![CDATA[UserHeaderValue]]></category>
		<category><![CDATA[WebService]]></category>

		<guid isPermaLink="false">http://klcin.tw/net/?p=122</guid>
		<description><![CDATA[本文介紹：在 WebService 用 SoapHeader 傳遞認證資訊並用 DESCryptoServiceProvider 加解密。 SoapHeader 變數圖解： 程式碼 SoapHeader public class UserHeader : System.Web.Services.Protocols.SoapHeader { public UserHeader() {} public string Username { get { return m_Username; } set { m_Username = value; } } private string m_Username; public string Password { get { return m_Password; } set { m_Password = value; } } [...]]]></description>
			<content:encoded><![CDATA[<p><strong>本文介紹</strong>：在 WebService 用 SoapHeader 傳遞認證資訊並用 DESCryptoServiceProvider 加解密。</p>
<p>SoapHeader 變數圖解：   <br /><a href="http://klcin.tw/net/wp-content/uploads/2009/04/snap250.png"><img title="SoapHeader 變數圖解" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="249" alt="SoapHeader 變數圖解" src="http://klcin.tw/net/wp-content/uploads/2009/04/snap250-thumb.png" width="404" border="0" /></a> </p>
<p> <span id="more-122"></span><br />
<h2>程式碼</h2>
<h3>SoapHeader</h3>
<p>
<pre class="brush: c#">public class UserHeader : System.Web.Services.Protocols.SoapHeader
{
  public UserHeader() {}

  public string Username { get { return m_Username; } set { m_Username = value; } }
  private string m_Username;

  public string Password { get { return m_Password; } set { m_Password = value; } }
  private string m_Password;
}</pre>
</p>
<h3>WebService</h3>
<p>
<pre class="brush: c#">// using System.Web.Services.Protocols;

public UserHeader m_userHeader = null;
public UserHeader UserHeader
{
  get { return m_userHeader; }
  set { m_userHeader = value; }
}

[SoapHeader(&quot;UserHeader&quot;, Direction = SoapHeaderDirection.InOut)]
[WebMethod]
public string ReturnLoginUser()
{
  string msg = &quot;err&quot;;
  if (m_userHeader != null)
  {
    string name = Decrypt(m_userHeader.Username, m_Key, m_IV);
    string pwd = Decrypt(m_userHeader.Password, m_Key, m_IV);

    msg = name + &quot;|&quot; + pwd;
  } // if

  return msg;
}</pre>
</p>
<h3>Client</h3>
<p>
<pre class="brush: c#">static void Main(string[] args)
{
  Service ws = new Service();

  string key=&quot;&quot;, iv=&quot;&quot;;
  ws.GetKeyAndIv(ref key, ref iv);

  UserHeader uh = new UserHeader();
  uh.Username = Encrypt(&quot;中文(abc)&quot;, key, iv);
  uh.Password = Encrypt(&quot;1234-ABC&quot;, key, iv);

  ws.UserHeaderValue = uh;
  Console.WriteLine(&quot;Return:{0}&quot;, ws.ReturnLoginUser());
  Console.ReadKey();
}</pre>
</p>
<h3>加密</h3>
<p>
<pre class="brush: c#">/// &lt;summary&gt;
/// DEC 加密法
/// &lt;/summary&gt;
/// &lt;param name=&quot;txt&quot;&gt;加密文本&lt;/param&gt;
/// &lt;param name=&quot;sKey&quot;&gt;加密金鑰&lt;/param&gt;
/// &lt;param name=&quot;sIV&quot;&gt;初始化向量&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public static string Encrypt(string txt, string key, string iv)
{
  System.Text.StringBuilder ret = new System.Text.StringBuilder();
  using (System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider())
  {
    byte[] data = System.Text.Encoding.Default.GetBytes(txt);
    des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(key);
    des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);

    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    {
      using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write))
      {
         cs.Write(data, 0, data.Length);
         cs.FlushFinalBlock();
      } // using

      foreach (byte b in ms.ToArray())
     {
        ret.AppendFormat(&quot;{0:X2}&quot;, b);
     } // foreach
    } // using
  } // using

  return ret.ToString();
}</pre>
</p>
<h3>解密</h3>
<p>
<pre class="brush: c#">/// &lt;summary&gt;
/// DEC 解密法
/// &lt;/summary&gt;
/// &lt;param name=&quot;txt&quot;&gt;解密的字串&lt;/param&gt;
/// &lt;param name=&quot;sKey&quot;&gt;加密金鑰&lt;/param&gt;
/// &lt;param name=&quot;sIV&quot;&gt;初始化向量&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public static string Decrypt(string txt, string key, string iv)
{
  using (System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider())
  {
    byte[] data = new byte[txt.Length / 2];
    for (int x = 0; x &lt; txt.Length / 2; x++)
    {
      int i = (Convert.ToInt32(txt.Substring(x * 2, 2), 16));
      data[x] = (byte)i;
    } // for

    des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(key);
    des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv);

    using (System.IO.MemoryStream ms =
      new System.IO.MemoryStream())
    {
      using (System.Security.Cryptography.CryptoStream cs =new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(),System.Security.Cryptography.CryptoStreamMode.Write))
      {
        try
        {
          cs.Write(data, 0, data.Length);
          cs.FlushFinalBlock();
          return System.Text.Encoding.Default.GetString(ms.ToArray());
        } // try
        catch
        {
          return &quot;&quot;;
        } // catch
      } // using
    } // using
  } // using
}</pre>
</p>
<p>&#160;</p>
<h4>檔案下載：</h4>
<ul>
<li><a hrerf='http://www.box.net/shared/y4cx0dk4k4' target='_blank'>程式碼（Box.net）</a></li>
</ul>
<h4>參考資料：</h4>
<ul>
<li>[MSDN] <a href="http://msdn.microsoft.com/zh-tw/library/system.web.services.protocols.soapheader(VS.80).aspx" target="_blank">SoapHeader 類別 (System.Web.Services.Protocols)</a></li>
<li>[MSDN] <a href="http://msdn.microsoft.com/zh-tw/library/system.security.cryptography.descryptoserviceprovider(VS.80).aspx" target="_blank">DESCryptoServiceProvider 類別 (System.Security.Cryptography)</a></li>
<li><a href="http://www.dotblogs.com.tw/dotjum/archive/2008/09/15/5379.aspx" target="_blank">[ASP.NET]為WebService多加上認證(SoapHeader) &#8211; Dotjum的分享空間</a>- 點部落</li>
<li><a href="http://www.dotblogs.com.tw/phoenix7765/archive/2008/08/30/5254.aspx" target="_blank">C# 加密(Encrypt) 解密(Decrypt) 使用DESCryptoServiceProvider &#8211; Phoenix.net 迷之殿</a>- 點部落</li>
<li><a href="http://www.cnblogs.com/Hetter/archive/2009/03/31/1426065.html" target="_blank">webservice 用戶驗證 加密 &#8211; Hetter</a> &#8211; 博客園</li>
<li><a href="http://www.cnblogs.com/jackyrong/archive/2007/05/23/757708.html" target="_blank">asp.net 2.0中傻瓜式使用soap header &#8211; jackyrong</a> &#8211; 博客園</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://klcin.tw/net/webservice-soapheader-descryptoserviceprovider/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>用__doPostBack在client端更新UpdatePanel</title>
		<link>http://klcin.tw/net/__dopostback-updatepanel</link>
		<comments>http://klcin.tw/net/__dopostback-updatepanel#comments</comments>
		<pubDate>Sun, 29 Mar 2009 13:56:20 +0000</pubDate>
		<dc:creator>klcintw</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[檔案下載]]></category>
		<category><![CDATA[UpdatePanel]]></category>
		<category><![CDATA[__doPostBack]]></category>

		<guid isPermaLink="false">http://klcin.tw/net/?p=119</guid>
		<description><![CDATA[本文介紹：用 __doPostBack 在 client 端更新 UpdatePanel，並用 Web Development Helper 觀察。 分析： 前 4 次連線是第一次載入時的頁面（default.aspx）本身及 3 個 AJAX 相關的 script 檔。Response：9,097。 第 5 次是按「選項1」。Response：1,042。 第 6 次是按「__doPostBack.1」。Response：1,042。 第 7 次是按「__doPostBack.2」。Response：1,042。 但 Request 的長度則略有不同。 用 UpdatePanel 的方式確實比原本整頁 postback 好。 但要注意：Page_Load 在普通的 postback&#160; 和 partial postback 都會執行（圖）。可以藉由 IsPostBack 和 IsInAsyncPostBack 來判斷兩者的差異。 可用 __doPostBack 第二個參數傳遞參數。 Server Side 流程 //========================== [...]]]></description>
			<content:encoded><![CDATA[<p><strong>本文介紹</strong>：用 __doPostBack 在 client 端更新 UpdatePanel，並用 <a href="http://projects.nikhilk.net/WebDevHelper" target="_blank">Web Development Helper</a> 觀察。</p>
<p><a href="http://klcin.tw/net/wp-content/uploads/2009/03/snap023.png"><img title="screen" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="402" alt="screen" src="http://klcin.tw/net/wp-content/uploads/2009/03/snap023-thumb.png" width="538" border="0" /></a> </p>
<p>分析：</p>
<ul>
<li>前 4 次連線是第一次載入時的頁面（default.aspx）本身及 3 個 AJAX 相關的 script 檔。Response：9,097。     <br />第 5 次是按「選項1」。Response：1,042。      <br />第 6 次是按「__doPostBack.1」。Response：1,042。      <br />第 7 次是按「__doPostBack.2」。Response：1,042。</li>
<li>但 Request 的長度則略有不同。</li>
</ul>
<p>用 UpdatePanel 的方式確實比原本整頁 postback 好。</p>
<p>但要注意：Page_Load 在普通的 postback&#160; 和 partial postback 都會執行（<a href="http://encosia.com/blog/media/images/updatepanel-life-cycle.png" target="_blank">圖</a>）。可以藉由 IsPostBack 和 IsInAsyncPostBack 來判斷兩者的差異。</p>
<p>可用 __doPostBack 第二個參數傳遞參數。</p>
<p> <span id="more-119"></span><br />
<h2>Server Side 流程</h2>
<p><font color="#004000">//==========================     <br />// 第一次載入</font>    <br />Page_Load    <br />&#160;&#160;&#160; IsPostBack=False    <br />&#160;&#160;&#160; IsInAsyncPostBack=False    <br />&#160;&#160;&#160; __EVENTTARGET=, __EVENTARGUMENT=    <br />UpdatePanel1_PreRender    <br /><font color="#004000">//==========================     <br />// 按「選項1」</font>    <br />Page_Load    <br />&#160;&#160;&#160; IsPostBack=<strong>True</strong>    <br />&#160;&#160;&#160; IsInAsyncPostBack=<strong>True</strong>    <br />&#160;&#160;&#160; __EVENTTARGET=, __EVENTARGUMENT=    <br />Button1_Click    <br />UpdatePanel1_PreRender    <br /><font color="#004000">==========================     <br />// 按「__doPostBack.1」</font>    <br />Page_Load    <br />&#160;&#160;&#160; IsPostBack=<strong>True</strong>    <br />&#160;&#160;&#160; IsInAsyncPostBack=<strong>True</strong>    <br />&#160;&#160;&#160; __EVENTTARGET=UpdatePanel1, __EVENTARGUMENT=參數1,參數2    <br />UpdatePanel1_PreRender    <br /><font color="#004000">==========================     <br />// 按「__doPostBack.2」</font>    <br />Page_Load    <br />&#160;&#160;&#160; IsPostBack=<strong>True</strong>    <br />&#160;&#160;&#160; IsInAsyncPostBack=<strong>True</strong>    <br />&#160;&#160;&#160; __EVENTTARGET=UpdatePanel1, __EVENTARGUMENT=    <br />UpdatePanel1_PreRender    <br /><font color="#004000">==========================     <br />// 按「清除」</font>    <br />Page_Load    <br />&#160;&#160;&#160; IsPostBack=<strong>True</strong>    <br />&#160;&#160;&#160; IsInAsyncPostBack=False    <br />&#160;&#160;&#160; __EVENTTARGET=UpdatePanel1, __EVENTARGUMENT=    <br />UpdatePanel1_PreRender</p>
<p>&#160;</p>
<h2>程式碼</h2>
<h3>default.aspx</h3>
<p>
<pre class="brush: html">&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;Default.aspx.cs&quot; Inherits=&quot;WebApplication1._Default&quot; %&gt;
&lt;%@ Register assembly=&quot;System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35&quot; namespace=&quot;System.Web.UI&quot; tagprefix=&quot;asp&quot; %&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt;
&lt;head runat=&quot;server&quot;&gt;
  &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
 &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;&lt;div&gt;
    &lt;asp:ScriptManager ID=&quot;ScriptManager1&quot; runat=&quot;server&quot;&gt;
    &lt;/asp:ScriptManager&gt;

    &lt;asp:UpdatePanel ID=&quot;UpdatePanel1&quot; runat=&quot;server&quot;
    onprerender=&quot;UpdatePanel1_PreRender&quot;&gt;
    &lt;ContentTemplate&gt;

    票數：&lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text=&quot;Label&quot;&gt;&lt;/asp:Label&gt;&lt;br /&gt;
    &lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;選項1&quot; onclick=&quot;Button1_Click&quot; /&gt;
    &lt;asp:Button ID=&quot;Button2&quot; runat=&quot;server&quot; Text=&quot;選項2&quot; onclick=&quot;Button2_Click&quot; /&gt;
    &lt;br /&gt;
    &lt;input type=&quot;button&quot; value=&quot;__doPostBack.1&quot; name=&quot;btn1&quot; id=&quot;btn1&quot; onclick=&quot;__doPostBack(&#039;UpdatePanel1&#039;, &#039;參數1,參數2&#039;);&quot; /&gt;
    &lt;input type=&quot;button&quot; value=&quot;__doPostBack.2&quot; name=&quot;btn2&quot; id=&quot;btn2&quot; onclick=&quot;__doPostBack(&#039;UpdatePanel1&#039;, &#039;&#039;);&quot; /&gt;
    &lt;br /&gt;裡面：&lt;%=DateTime.Now.ToString() %&gt;
    &lt;/ContentTemplate&gt;

    &lt;/asp:UpdatePanel&gt;
    &lt;asp:Button ID=&quot;btnReset&quot; runat=&quot;server&quot; Text=&quot;清除&quot; onclick=&quot;btnReset_Click&quot; /&gt;
    &lt;br /&gt;外面：&lt;%=DateTime.Now.ToString() %&gt;
&lt;/div&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;
</pre>
</p>
<h3>default.aspx.cs</h3>
<p>
<pre class="brush: c#">using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
 public partial class _Default : System.Web.UI.Page
 {
  protected void Page_Load(object sender, EventArgs e)
  {
   Debug.WriteLine(&quot;==========================rnPage_Load&quot;);
   Debug.WriteLine(string.Format(&quot;tIsPostBack={0}&quot;, this.IsPostBack));
   Debug.WriteLine(string.Format(&quot;tIsInAsyncPostBack={0}&quot;, this.ScriptManager1.IsInAsyncPostBack));
   Debug.WriteLine(string.Format(&quot;t__EVENTTARGET={0}, __EVENTARGUMENT={1}&quot;, Request[&quot;__EVENTTARGET&quot;], Request[&quot;__EVENTARGUMENT&quot;]));

   if (Session[&quot;0&quot;] == null)
   {
    Session[&quot;0&quot;] = 0;
    Session[&quot;1&quot;] = 0;
   } // if

   ShowResult();
  }

  protected void Button1_Click(object sender, EventArgs e)
  {
   Debug.WriteLine(&quot;Button1_Click&quot;);
   Session[&quot;0&quot;] = Int32.Parse(Session[&quot;0&quot;].ToString()) + 1;
   ShowResult();
  }

  protected void Button2_Click(object sender, EventArgs e)
  {
   Debug.WriteLine(&quot;Button2_Click&quot;);
   Session[&quot;1&quot;] = Int32.Parse(Session[&quot;1&quot;].ToString()) + 1;
   ShowResult();
  }

  private void ShowResult()
  {
   this.Label1.Text = string.Format(&quot;1:{0}, 2:{1}&quot;, Session[&quot;0&quot;], Session[&quot;1&quot;]);
  }

  protected void btnReset_Click(object sender, EventArgs e)
  {
   Session[&quot;0&quot;] = 0;
   Session[&quot;1&quot;] = 0;
   ShowResult();
  }

  protected void UpdatePanel1_PreRender(object sender, EventArgs e)
  {
   Debug.WriteLine(&quot;UpdatePanel1_PreRender&quot;);
  }
 }
}
</pre>
</p>
<p>&#160;</p>
<h4>檔案下載</h4>
<ul>
<li><a href='http://www.box.net/shared/etucrnuf24' target='_blank'>程式碼（Box.net）</a></li>
</ul>
<h4>參考資料</h4>
<ul>
<li><a href="http://encosia.com/2007/10/24/are-you-making-these-3-common-aspnet-ajax-mistakes/" target="_blank">Are you making these 3 common ASP.NET AJAX mistakes? | Encosia</a></li>
<li><a href="http://encosia.com/2007/07/13/easily-refresh-an-updatepanel-using-javascript/" target="_blank">Easily refresh an UpdatePanel, using JavaScript | Encosia</a></li>
<li><a href="http://forums.asp.net/p/1067215/1550710.aspx" target="_blank">UpdatePanel and __doPostBack &#8211; ASP.NET Forums</a></li>
<li>[MSDN] <a href="http://msdn.microsoft.com/en-us/library/aa720099(vs.71).aspx" target="_blank">Generating Client-Side Script for Postback</a></li>
<li>[MSDN] <a href="http://msdn.microsoft.com/en-us/library/ms178472.aspx" target="_blank">ASP.NET Page Life Cycle Overview</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://klcin.tw/net/__dopostback-updatepanel/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

