posts - 5,  comments - 5,  trackbacks - 0

using Microsoft.SharePoint.WebControls;
using Microsoft.Web.UI.WebControls;
private void Button1_Click(object sender, System.EventArgs e)
  {
   //模拟用户
   //WindowsImpersonationContext wic = CreateIdentity("ssa_sps03","paicdom","spstestA1").Impersonate();
   List list = new List();
   list.AddList(tb_NewsTitle.Text);
   //wic.Undo();
  }

  protected static WindowsIdentity CreateIdentity(string User, string Domain, string Password)
  {
   // The Windows NT user token.
   IntPtr tokenHandle = new IntPtr(0);

   const int LOGON32_PROVIDER_DEFAULT = 0;
   const int LOGON32_LOGON_NETWORK = 3;

   tokenHandle = IntPtr.Zero;

   // Call LogonUser to obtain a handle to an access token.
   bool returnValue = LogonUser(User, Domain, Password,
    LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
    ref tokenHandle);

   if (false == returnValue)
   {
    int ret = Marshal.GetLastWin32Error();
    throw new Exception("LogonUser failed with error code: " +  ret);
   }

   System.Diagnostics.Debug.WriteLine("Created user token: " + tokenHandle);

   //The WindowsIdentity class makes a new copy of the token.
   //It also handles calling CloseHandle for the copy.
   WindowsIdentity id = new WindowsIdentity(tokenHandle);
   CloseHandle(tokenHandle);
   return id;
  }

  [DllImport("advapi32.dll", SetLastError=true)]
  private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
   int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

  [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
  private extern static bool CloseHandle(IntPtr handle);
 }

posted on 2006-12-04 10:31 曾科 阅读(388) 评论(0)  编辑  收藏 所属分类: C#

只有注册用户登录后才能发表评论。


网站导航:
 
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(3)

随笔档案

文章分类

文章档案

相册

.net

搜索

  •  

最新评论

阅读排行榜

评论排行榜