/// <summary> /// Allocates a COSERVERINFO structure. /// </summary> public COSERVERINFO Allocate(string hostName, NetworkCredential credential) { string userName = null; string password = null; string domain = null; if (credential != null) { userName = credential.UserName; password = credential.Password; domain = credential.Domain; } m_hUserName = GCHandle.Alloc(userName, GCHandleType.Pinned); m_hPassword = GCHandle.Alloc(password, GCHandleType.Pinned); m_hDomain = GCHandle.Alloc(domain, GCHandleType.Pinned); m_hIdentity = new GCHandle(); if (userName != null && userName != String.Empty) { COAUTHIDENTITY identity = new COAUTHIDENTITY(); identity.User = m_hUserName.AddrOfPinnedObject(); identity.UserLength = (uint)((userName != null) ? userName.Length : 0); identity.Password = m_hPassword.AddrOfPinnedObject(); identity.PasswordLength = (uint)((password != null) ? password.Length : 0); identity.Domain = m_hDomain.AddrOfPinnedObject(); identity.DomainLength = (uint)((domain != null) ? domain.Length : 0); identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE; m_hIdentity = GCHandle.Alloc(identity, GCHandleType.Pinned); } COAUTHINFO authInfo = new COAUTHINFO(); authInfo.dwAuthnSvc = RPC_C_AUTHN_WINNT; authInfo.dwAuthzSvc = RPC_C_AUTHZ_NONE; authInfo.pwszServerPrincName = IntPtr.Zero; authInfo.dwAuthnLevel = RPC_C_AUTHN_LEVEL_CONNECT; authInfo.dwImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE; authInfo.pAuthIdentityData = (m_hIdentity.IsAllocated) ? m_hIdentity.AddrOfPinnedObject() : IntPtr.Zero; authInfo.dwCapabilities = EOAC_NONE; m_hAuthInfo = GCHandle.Alloc(authInfo, GCHandleType.Pinned); COSERVERINFO serverInfo = new COSERVERINFO(); serverInfo.pwszName = hostName; serverInfo.pAuthInfo = (credential != null) ? m_hAuthInfo.AddrOfPinnedObject() : IntPtr.Zero; serverInfo.dwReserved1 = 0; serverInfo.dwReserved2 = 0; return(serverInfo); }
/// <summary> /// Allocates a COSERVERINFO structure. /// </summary> public COSERVERINFO Allocate(string hostName, string username, string password, string domain) { // initialize server info structure. COSERVERINFO serverInfo = new COSERVERINFO(); serverInfo.pwszName = hostName; serverInfo.pAuthInfo = IntPtr.Zero; serverInfo.dwReserved1 = 0; serverInfo.dwReserved2 = 0; // no authentication for default identity if (String.IsNullOrEmpty(username)) { return(serverInfo); } m_hUserName = GCHandle.Alloc(username, GCHandleType.Pinned); m_hPassword = GCHandle.Alloc(password, GCHandleType.Pinned); m_hDomain = GCHandle.Alloc(domain, GCHandleType.Pinned); m_hIdentity = new GCHandle(); // create identity structure. COAUTHIDENTITY authIdentity = new COAUTHIDENTITY(); authIdentity.User = m_hUserName.AddrOfPinnedObject(); authIdentity.UserLength = (uint)((username != null) ? username.Length : 0); authIdentity.Password = m_hPassword.AddrOfPinnedObject(); authIdentity.PasswordLength = (uint)((password != null) ? password.Length : 0); authIdentity.Domain = m_hDomain.AddrOfPinnedObject(); authIdentity.DomainLength = (uint)((domain != null) ? domain.Length : 0); authIdentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE; m_hIdentity = GCHandle.Alloc(authIdentity, GCHandleType.Pinned); // create authorization info structure. COAUTHINFO authInfo = new COAUTHINFO(); authInfo.dwAuthnSvc = RPC_C_AUTHN_WINNT; authInfo.dwAuthzSvc = RPC_C_AUTHZ_NONE; authInfo.pwszServerPrincName = IntPtr.Zero; authInfo.dwAuthnLevel = RPC_C_AUTHN_LEVEL_CONNECT; authInfo.dwImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE; authInfo.pAuthIdentityData = m_hIdentity.AddrOfPinnedObject(); authInfo.dwCapabilities = EOAC_NONE; // EOAC_DYNAMIC_CLOAKING; m_hAuthInfo = GCHandle.Alloc(authInfo, GCHandleType.Pinned); // update server info structure. serverInfo.pAuthInfo = m_hAuthInfo.AddrOfPinnedObject(); return(serverInfo); }
// Methods public COSERVERINFO Allocate(string hostName, NetworkCredential credential) { string userName = null; string password = null; string domain = null; if (credential != null) { userName = credential.UserName; password = credential.Password; domain = credential.Domain; } this.m_hUserName = GCHandle.Alloc(userName, GCHandleType.Pinned); this.m_hPassword = GCHandle.Alloc(password, GCHandleType.Pinned); this.m_hDomain = GCHandle.Alloc(domain, GCHandleType.Pinned); this.m_hIdentity = new GCHandle(); if ((userName != null) && (userName != string.Empty)) { COAUTHIDENTITY coauthidentity = new COAUTHIDENTITY { User = this.m_hUserName.AddrOfPinnedObject(), UserLength = (userName != null) ? ((uint)userName.Length) : 0, Password = this.m_hPassword.AddrOfPinnedObject(), PasswordLength = (password != null) ? ((uint)password.Length) : 0, Domain = this.m_hDomain.AddrOfPinnedObject(), DomainLength = (domain != null) ? ((uint)domain.Length) : 0, Flags = 2 }; this.m_hIdentity = GCHandle.Alloc(coauthidentity, GCHandleType.Pinned); } COAUTHINFO coauthinfo = new COAUTHINFO { dwAuthnSvc = 10, dwAuthzSvc = 0, pwszServerPrincName = IntPtr.Zero, dwAuthnLevel = 2, dwImpersonationLevel = 3, pAuthIdentityData = this.m_hIdentity.IsAllocated ? this.m_hIdentity.AddrOfPinnedObject() : IntPtr.Zero, dwCapabilities = 0 }; this.m_hAuthInfo = GCHandle.Alloc(coauthinfo, GCHandleType.Pinned); return(new COSERVERINFO { pwszName = hostName, pAuthInfo = (credential != null) ? this.m_hAuthInfo.AddrOfPinnedObject() : IntPtr.Zero, dwReserved1 = 0, dwReserved2 = 0 }); }
/// <summary> /// Sets the security settings for the proxy. /// </summary> public static void SetProxySecurity(object server, UserIdentity user) { // allocate the GCHandle hUserName = GCHandle.Alloc(user.Username, GCHandleType.Pinned); GCHandle hPassword = GCHandle.Alloc(user.Password, GCHandleType.Pinned); GCHandle hDomain = GCHandle.Alloc(user.Domain, GCHandleType.Pinned); GCHandle hIdentity = new GCHandle(); // create identity structure. COAUTHIDENTITY authIdentity = new COAUTHIDENTITY(); authIdentity.User = hUserName.AddrOfPinnedObject(); authIdentity.UserLength = (uint)((user.Username != null) ? user.Username.Length : 0); authIdentity.Password = hPassword.AddrOfPinnedObject(); authIdentity.PasswordLength = (uint)((user.Password != null) ? user.Password.Length : 0); authIdentity.Domain = hDomain.AddrOfPinnedObject(); authIdentity.DomainLength = (uint)((user.Domain != null) ? user.Domain.Length : 0); authIdentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE; hIdentity = GCHandle.Alloc(authIdentity, GCHandleType.Pinned); try { SetProxySecurity(server, hIdentity.AddrOfPinnedObject()); } finally { hUserName.Free(); hPassword.Free(); hDomain.Free(); hIdentity.Free(); } }
/// <summary> /// Allocates a COSERVERINFO structure. /// </summary> public COSERVERINFO Allocate(string hostName, UserIdentity identity) { // initialize server info structure. COSERVERINFO serverInfo = new COSERVERINFO(); serverInfo.pwszName = hostName; serverInfo.pAuthInfo = IntPtr.Zero; serverInfo.dwReserved1 = 0; serverInfo.dwReserved2 = 0; // no authentication for default identity if (UserIdentity.IsDefault(identity)) { return serverInfo; } m_hUserName = GCHandle.Alloc(identity.Username, GCHandleType.Pinned); m_hPassword = GCHandle.Alloc(identity.Password, GCHandleType.Pinned); m_hDomain = GCHandle.Alloc(identity.Domain, GCHandleType.Pinned); m_hIdentity = new GCHandle(); // create identity structure. COAUTHIDENTITY authIdentity = new COAUTHIDENTITY(); authIdentity.User = m_hUserName.AddrOfPinnedObject(); authIdentity.UserLength = (uint)((identity.Username != null)?identity.Username.Length:0); authIdentity.Password = m_hPassword.AddrOfPinnedObject(); authIdentity.PasswordLength = (uint)((identity.Password != null)?identity.Password.Length:0); authIdentity.Domain = m_hDomain.AddrOfPinnedObject(); authIdentity.DomainLength = (uint)((identity.Domain != null)?identity.Domain.Length:0); authIdentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE; m_hIdentity = GCHandle.Alloc(authIdentity, GCHandleType.Pinned); // create authorization info structure. COAUTHINFO authInfo = new COAUTHINFO(); authInfo.dwAuthnSvc = RPC_C_AUTHN_WINNT; authInfo.dwAuthzSvc = RPC_C_AUTHZ_NONE; authInfo.pwszServerPrincName = IntPtr.Zero; authInfo.dwAuthnLevel = RPC_C_AUTHN_LEVEL_CONNECT; authInfo.dwImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE; authInfo.pAuthIdentityData = m_hIdentity.AddrOfPinnedObject(); authInfo.dwCapabilities = EOAC_NONE; // EOAC_DYNAMIC_CLOAKING; m_hAuthInfo = GCHandle.Alloc(authInfo, GCHandleType.Pinned); // update server info structure. serverInfo.pAuthInfo = m_hAuthInfo.AddrOfPinnedObject(); return serverInfo; }