日期:2009-08-10  浏览次数:20495 次

不管是ASP.net、web service还是window service,程序运行的时候只有本地计算机的部分权限,有时候需要更大的权限,比如读写某台服务器或域中的一台计算机上的文件等,这就需要更大的权限,比如域帐户权限。


通过获取不同身份的WindowsImpersonationContext对象,可以模拟不同用户登陆,请看我生成的NetworkSecurity类的
public static WindowsImpersonationContext ImpersonateUser(string strDomain,
string strLogin,

string strPwd,

LOGOnType LOGOnType,

LOGOnProvider LOGOnProvider);

附NetworkSecurity.cs源代码如下:

/*
* Author : TongWei
* Date : 2005-1-25
* Rights : China Netwave Inc.@2005
*/

using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;

namespace CNW.OMP.Common.Utility
{
public enum LOGOnType : int
{
/// <summary>
/// This LOGOn type is intended for users who will be interactively using the computer, such as a user
/// being logged on by a terminal server, remote shell, or similar process. This LOGOn type has the
/// additional expense of caching LOGOn information for disconnected operation, and is therefore
/// inappropriate for some client/server applications, such as a mail server.
/// </summary>
LOGON32_LOGON_INTERACTIVE = 2,

/// <summary>
/// This LOGOn type is intended for high performance servers to authenticate clear text passwords.
/// The LOGOnUser function does not cache credentials for this LOGOn type.
/// </summary>
LOGON32_LOGON_NETWORK = 3,

/// <summary>
/// This LOGOn type is intended for batch servers, where processes may be executing on behalf of a user
/// without their direct intervention; or for higher performance servers that process many clear-text
/// authentication attempts at a time, such as mail or web servers. The LOGOnUser function does not cache
/// credentials for this LOGOn type.
/// </summary>
LOGON32_LOGON_BATCH = 4,

/// <summary>
/// Indicates a service-type LOGOn. The account provided must have the service privilege enabled.
/// </summary>
LOGON32_LOGON_SERVICE = 5,

/// <summary>
/// This LOGOn type is intended for GINA DLLs logging on users who will be interactively using the computer.
/// This LOGOn type allows a unique audit record to be generated that shows when the workstation was unlocked.
/// </summary>
LOGON32_LOGON_UNLOCK = 7,

/// <summary>
/// Windows XP/2000: This LOGOn type preserves the name and password in the authentication packages,
/// allowing the server to make connections to other network servers while impersonating the client.
/// This allows a server to accept clear text credentials from a client, call LOGOnUser, verify that
/// the user can access the system across the network, and still communicate with other servers.
/// </summary>
LOGON32_LOGON_NETWORK_CLEARTEXT = 8,

/// <summary>
/// Windows XP/2000: This LOGOn type allows the caller to clone its current token and specify new credentials
/// for outbound connections. The new LOGOn session has the same local identity, but uses different credentials
/// for other network connections.
/// This LOGOn type is supported only by the LOGON32_PROVIDER_WINNT50 LOGOn provider.
/// </summary>
LOGON32_LOGON_NEW_CREDENTIALS = 9
};

public enum LOGOnProvider : int
{
/// <summary>
/// Use the standard LOGOn provider for the system. The default security provider is NTLM.
/// Wi