Web Parts, Impersonate and Security Policy, Part 1

Web Parts, Impersonate and Security Policy

Part 1

 

Written by: Rickie Lee (rickieleemail at yahoo.com)

在开发某些Web Parts的时候,需要访问一些对象或数据。因为SPS进行严格的安全访问控制,有些用户授权实现这些操作,但是其他用户则没有授权,同时也不可能授予这些权限给所有人。

 

在这种情况下,当用户不具有权限去执行这些操作时,SPS将让IE弹出login窗口,让用户输入credentials凭据。为了避免这种不友好的间断,使用户平滑访问特权操作和资源,需要实现定制的impersonation方案。这样,执行这些操作的代码需要在合适的account帐户环境下,该帐户授权执行必要的操作。并且在这些操作执行完成后,用户的帐户需要返回至原来的domain帐户。

 

如下的示例代码(C#)用来显式创建System.Security.Principal.WindowsIdentity实例对象,标识新的account

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;

 

          // Initialize token object

          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);

 

          // Check for failure

          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);

 

Web Parts调用示例代码:

// Assign new account information

string strUID = "Rickie";

string strPassword = "rickieleemail at yahoo.com";

string strDomain = "Your_Domain_Name";

 

// Change the context

WindowsIdentity objIdentity = CreateIdentity(strUID, strDomain, strPassword);

objContext = objIdentity.Impersonate();

// Perform actions that require higher permission level

......

// Tear down context

objContext.Undo();

 

首先调用CreateIdentity()方法,创建System.Security.Principal.WindowsIdentity实例对象,该account具有合适的访问权限。然后调用WindowsIdentity对象的Impersonate()方法,开始扮演(impersonate)新的windows identity,执行需要特权的方法或资源。完成上述操作后,停止impersonate并返回到用户原来的身份(account)。

 

***

未完,继续part2.

 

 

posted @   Rickie  阅读(1798)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示