/// <summary>
/// It establishes a new Session Context with the server by calling native methods.
/// </summary>
/// <param name="server">The server name.</param>
/// <param name="domain">The domain the server is deployed</param>
/// <param name="userName">The domain account name.</param>
/// <param name="userDN">User's distinguished name (DN).</param>
/// <param name="password">>user Password.</param>
/// <param name="userSpn">User's SPN.</param>
/// <param name="mapiContext">The default parameters for rpc call, such as authentication level, authentication method, whether to compress request.</param>
/// <param name="options">proxy attribute</param>
/// <returns>n success, the server MUST return a unique value to be used as a CXH. This unique value serves as the CXH for the client</returns>
public IntPtr Connect(string server, string domain, string userName, string userDN, string password, string userSpn, MapiContext mapiContext, string options)
{
// The default parameter for out session handle.
IntPtr pcxh = IntPtr.Zero;
// CreateIdentity
NativeMethods.CreateIdentity(
domain,
userName,
password);
uint status = NativeMethods.BindToServer(server, mapiContext.AuthenLevel, mapiContext.AuthenService, mapiContext.TransportSequence.ToLower(), mapiContext.RpchUseSsl, mapiContext.RpchAuthScheme, userSpn, options, mapiContext.SetUuid);
if (status != 0)
{
throw new Exception("Could not create binding handle with server");
}
this.bindingHandle = NativeMethods.GetBindHandle();
int waitTime = int.Parse(Common.GetConfigurationPropertyValue("WaitTime", this.site));
int maxRetryCount = int.Parse(Common.GetConfigurationPropertyValue("ConnectRetryCount", this.site));
int retryCount = 0;
do
{
status = this.Connect_Internal(ref pcxh, userDN, ref mapiContext);
if (status >= 1700 && status <= 1799)
{
// If status is between 1700 and 1799, try to connect again.
retryCount++;
System.Threading.Thread.Sleep(waitTime);
if (retryCount > 0)
{
this.site.Log.Add(LogEntryKind.Comment, "Can't connect to RPC server, will try to connect the server again. Current retry number is {0}.", retryCount);
}
}
else
{
break;
}
}while (retryCount < maxRetryCount);
if (status != 0)
{
string errorCodeHexString = "0x" + status.ToString("X8");
string errorCodeMeaning = GetErrorCodeMeaning(errorCodeHexString);
string errorCodeDescription = string.Empty;
if (string.IsNullOrEmpty(errorCodeMeaning))
{
errorCodeDescription = string.Format("Error code '{0}' is not defined in protocol MS-OXCRPC. The error message is: {1}", errorCodeHexString, (new Win32Exception((int)status)).ToString());
}
else
{
errorCodeDescription = string.Format("Error code '{0}' is defined in protocol MS-OXCRPC as: {1}", errorCodeHexString, errorCodeMeaning);
}
this.site.Assert.Fail("Connect method returned an error: {0}. {1}", errorCodeHexString, errorCodeDescription);
}
return(pcxh);
}