Security.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2008
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. //*
  9. //* security.sln
  10. //*
  11. //* Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  12. //* Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  13. //*
  14. //* This sample illustrates the use of the logon and logoff methods.
  15. //*
  16. using System;
  17. using System.Web.Services.Protocols;
  18. using SamplesCommon;
  19. using cognosdotnet_10_2;
  20. namespace Security
  21. {
  22. /// <summary>
  23. /// Summary description for Security.
  24. /// </summary>
  25. class Security
  26. {
  27. /// <summary>
  28. /// The main entry point for the application.
  29. /// </summary>
  30. ///
  31. public Security(){}
  32. static void Main(string[] args)
  33. {
  34. string cBIUrl = "";
  35. contentManagerService1 cBIServer = null;
  36. SamplesConnect connectDlg = new SamplesConnect();
  37. SecurityDlg securityDlgObject = new SecurityDlg();
  38. // The savedUserName and savedNamespace variables are only used for convenience,
  39. // so that the user name and the namespace can be remembered when prompting for
  40. // logon multiple times.
  41. string savedUserName = "";
  42. string savedNamespace = "";
  43. if (args.GetLength(0) == 0 )
  44. {
  45. // GUI mode
  46. connectDlg.ShowDialog();
  47. if (connectDlg.IsConnectedToCBI() == true)
  48. {
  49. cBIServer = connectDlg.CBICMS;
  50. cBIUrl = connectDlg.CBIURL;
  51. savedUserName = connectDlg.getUserName();
  52. savedNamespace = connectDlg.getNamespace();
  53. securityDlgObject.setConnection(connectDlg, cBIUrl, savedUserName, savedNamespace);
  54. securityDlgObject.ShowDialog();
  55. }
  56. }
  57. }
  58. public bool getLogonInfo(SamplesConnect cBIConnection, ref string result)
  59. {
  60. if ( cBIConnection == null)
  61. {
  62. result = "...the Server connection is invalid.\n";
  63. return false;
  64. }
  65. try
  66. {
  67. account myAccount = new account();
  68. baseClass[] bc = new baseClass[1];
  69. propEnum[] props =
  70. new propEnum[] { propEnum.searchPath, propEnum.defaultName };
  71. searchPathMultipleObject homeSearchPath = new searchPathMultipleObject();
  72. homeSearchPath.Value = "~";
  73. bc = cBIConnection.CBICMS.query(homeSearchPath, props, new sort[] {}, new queryOptions());
  74. if ( (bc != null) && (bc.GetLength(0) >0) )
  75. {
  76. for (int i=0; i<bc.GetLength(0); i++)
  77. {
  78. myAccount.defaultName = new tokenProp();
  79. myAccount.defaultName.value = bc[i].defaultName.value;
  80. myAccount.searchPath = new stringProp();
  81. myAccount.searchPath.value = bc[i].searchPath.value;
  82. }
  83. result += "You are currently logged on as: " + myAccount.defaultName.value;
  84. result += "\nYour searchPath is: " + myAccount.searchPath.value;
  85. }
  86. }
  87. catch(SoapException ex)
  88. {
  89. SamplesException.ShowExceptionMessage( ex, true, "Security Samples - getLogonInfo()" );
  90. result += "getLogonInfo() Failed with error:" + ex.Message;
  91. return false;
  92. }
  93. catch(System.Exception ex)
  94. {
  95. SamplesException.ShowExceptionMessage( ex.Message, true, "Security Samples - getLogonInfo()" );
  96. result += "getLogonInfo() Failed with error:" + ex.Message;
  97. return false;
  98. }
  99. return true;
  100. }
  101. public bool doLogon(SamplesConnect cBIConnection, string savedUserName, string savedNamespace, ref string result)
  102. {
  103. if ( cBIConnection == null)
  104. {
  105. result = "...the Server connection is invalid.\n";
  106. return false;
  107. }
  108. getAccountInfo:
  109. account myAccount = new account();
  110. baseClass[] bc = new baseClass[1];
  111. propEnum[] props =
  112. new propEnum[] { propEnum.searchPath, propEnum.defaultName };
  113. searchPathMultipleObject homeSearchPath = new searchPathMultipleObject();
  114. homeSearchPath.Value = "~";
  115. try
  116. {
  117. bc = cBIConnection.CBICMS.query(homeSearchPath, props, new sort[] {}, new queryOptions());
  118. }
  119. catch(System.Exception ex)
  120. {
  121. // Anonymous is OFF, attempt to log on.
  122. SamplesLogon logon = new SamplesLogon( cBIConnection );
  123. logon.setUserName(savedUserName);
  124. logon.setNamespace(savedNamespace);
  125. logon.ShowDialog( );
  126. if( !logon.loggedOn )
  127. {
  128. result = "...unable to log.";
  129. return false;
  130. }
  131. else
  132. {
  133. savedUserName = logon.getUserName();
  134. goto getAccountInfo;
  135. }
  136. }
  137. if (bc != null)
  138. {
  139. // Anonymous is ON.
  140. getLogonInfo(cBIConnection, ref result);
  141. }
  142. return true;
  143. }
  144. public bool doLogonAs(SamplesConnect cBIConnection, string savedUserName, string savedNamespace, ref string result)
  145. {
  146. if ( cBIConnection == null)
  147. {
  148. result = "...the Server connection is invalid.\n";
  149. return false;
  150. }
  151. doLogoff(cBIConnection, ref result);
  152. result += "\n";
  153. account myAccount = new account();
  154. baseClass[] bc = new baseClass[1];
  155. propEnum[] props =
  156. new propEnum[] { propEnum.searchPath, propEnum.defaultName };
  157. searchPathMultipleObject homeSearchPath = new searchPathMultipleObject();
  158. homeSearchPath.Value = "~";
  159. try
  160. {
  161. bc = cBIConnection.CBICMS.query(homeSearchPath, props, new sort[] {}, new queryOptions());
  162. SamplesLogon logon = new SamplesLogon( cBIConnection );
  163. logon.setUserName(savedUserName);
  164. logon.setNamespace(savedNamespace);
  165. logon.setSwitchUserMode(true);
  166. logon.ShowDialog( );
  167. if( logon.loggedOn )
  168. {
  169. getLogonInfo(cBIConnection, ref result);
  170. return true;
  171. }
  172. else
  173. {
  174. result = "...unable to log on.";
  175. return false;
  176. }
  177. }
  178. catch(System.Exception ex)
  179. {
  180. // Anonymous is OFF, attempt to log on.
  181. SamplesLogon logon = new SamplesLogon( cBIConnection );
  182. logon.setUserName(savedUserName);
  183. logon.setNamespace(savedNamespace);
  184. logon.ShowDialog( );
  185. if( logon.loggedOn )
  186. {
  187. getLogonInfo(cBIConnection, ref result);
  188. return true;
  189. }
  190. else
  191. {
  192. result = "...unable to log on.";
  193. return false;
  194. }
  195. }
  196. }
  197. public bool doLogoff(SamplesConnect cBIConnection, ref string result)
  198. {
  199. if ( cBIConnection == null)
  200. {
  201. result += "\nInvalid parameter passed to function logon.";
  202. return false;
  203. }
  204. // sn_dg_sdk_method_contentManagerService_logoff_start_0
  205. cBIConnection.CBICMS.logoff();
  206. // sn_dg_sdk_method_contentManagerService_logoff_end_0
  207. cBIConnection.CBICMS.biBusHeaderValue = new biBusHeader();
  208. result = "You have successfully logged off.";
  209. return true;
  210. }
  211. }
  212. }