public static int BuildCredsContext(string sUPNName, string sPassword, string sDomain, out IntPtr CrdesCache) { int ret = -1; string sCredsCache = string.Empty; string sPrinicalName = string.Empty; IntPtr pAccessToken = IntPtr.Zero; CRED_CONTEXT creds = new CRED_CONTEXT(); CrdesCache = IntPtr.Zero; try { Logger.Log("sUPNName :" + sUPNName); if (String.IsNullOrEmpty(sUPNName) || sUPNName.Equals("administrator", StringComparison.InvariantCultureIgnoreCase)) { ret = Krb5GetDomainCredsCache(sUPNName, sDomain, sPassword); Logger.Log(string.Format("Krb5CredsCache.BuildCredsContext: Krb5GetDomainCredsCache(sDomain={0}) ret={1}", sDomain, ret.ToString())); if (ret != 0) { return(ret); } ret = Krb5GetSystemCachePath(out sCredsCache); Logger.Log(string.Format("Krb5CredsCache.BuildCredsContext: Krb5GetSystemCachePath(out sCredsCache={0}) ret={1}", sCredsCache, ret.ToString())); if (ret != 0) { return(ret); } } else { ret = Krb5GetUserCachePath(sUPNName, out sCredsCache); Logger.Log(string.Format("Krb5CredsCache.BuildCredsContext: Krb5GetUserCachePath(sUPNName={0}, out sCredsCache={1}) ret={1}", sUPNName, sCredsCache, ret.ToString())); if (ret != 0) { return(ret); } } ret = Krb5GetPrincipalName(sCredsCache, out sPrinicalName); Logger.Log(string.Format("Krb5CredsCache.BuildCredsContext: Krb5GetPrincipalName(sCredsCache={0}, out sPrinicalName={1}) ret={2}", sCredsCache, sPrinicalName, ret.ToString())); if (ret != 0) { return(ret); } ret = apiLwIoCreateKrb5AccessTokenA(sPrinicalName, sCredsCache, out pAccessToken); Logger.Log(string.Format("Krb5CredsCache.BuildCredsContext: apiLwIoCreateKrb5AccessTokenA(sPrinicalName={0}, sCredsCache={1},out pAccessToken={2}) ret={3}", sPrinicalName, sCredsCache, pAccessToken.ToInt32().ToString(), ret.ToString())); if (ret != 0) { return(ret); } creds.pszCachePath = sCredsCache; creds.pszPrincipalName = sPrinicalName; creds.pAccessToken = pAccessToken; CrdesCache = IntPtr.Zero; CrdesCache = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CRED_CONTEXT))); Marshal.StructureToPtr(creds, CrdesCache, false); return(ret); } catch (Exception ex) { Logger.LogException("Krb5CredsCache.BuildCredsContext", ex); } return(ret); }
public static int BuildCredsContext(string sUPNName, string sPassword, string sDomain, out IntPtr CrdesCache) { int ret = -1; string sCredsCache = string.Empty; string sPrinicalName = string.Empty; IntPtr pAccessToken = IntPtr.Zero; CRED_CONTEXT creds = new CRED_CONTEXT(); CrdesCache = IntPtr.Zero; try { Logger.Log("sUPNName :" + sUPNName); if (String.IsNullOrEmpty(sUPNName) || sUPNName.Equals("administrator", StringComparison.InvariantCultureIgnoreCase)) { ret = Krb5GetDomainCredsCache(sUPNName, sDomain, sPassword); Logger.Log(string.Format("Krb5CredsCache.BuildCredsContext: Krb5GetDomainCredsCache(sDomain={0}) ret={1}", sDomain, ret.ToString())); if (ret != 0) { return ret; } ret = Krb5GetSystemCachePath(out sCredsCache); Logger.Log(string.Format("Krb5CredsCache.BuildCredsContext: Krb5GetSystemCachePath(out sCredsCache={0}) ret={1}", sCredsCache, ret.ToString())); if (ret != 0) { return ret; } } else { ret = Krb5GetUserCachePath(sUPNName, out sCredsCache); Logger.Log(string.Format("Krb5CredsCache.BuildCredsContext: Krb5GetUserCachePath(sUPNName={0}, out sCredsCache={1}) ret={1}", sUPNName, sCredsCache, ret.ToString())); if (ret != 0) { return ret; } } ret = Krb5GetPrincipalName(sCredsCache, out sPrinicalName); Logger.Log(string.Format("Krb5CredsCache.BuildCredsContext: Krb5GetPrincipalName(sCredsCache={0}, out sPrinicalName={1}) ret={2}", sCredsCache, sPrinicalName, ret.ToString())); if (ret != 0) { return ret; } ret = apiLwIoCreateKrb5AccessTokenA(sPrinicalName, sCredsCache, out pAccessToken); Logger.Log(string.Format("Krb5CredsCache.BuildCredsContext: apiLwIoCreateKrb5AccessTokenA(sPrinicalName={0}, sCredsCache={1},out pAccessToken={2}) ret={3}", sPrinicalName, sCredsCache, pAccessToken.ToInt32().ToString(), ret.ToString())); if (ret != 0) { return ret; } creds.pszCachePath = sCredsCache; creds.pszPrincipalName = sPrinicalName; creds.pAccessToken = pAccessToken; CrdesCache = IntPtr.Zero; CrdesCache = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CRED_CONTEXT))); Marshal.StructureToPtr(creds, CrdesCache, false); return ret; } catch (Exception ex) { Logger.LogException("Krb5CredsCache.BuildCredsContext", ex); } return ret; }