PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/CMSWebParts/Membership/Registration/LiveIDUsersRequiredData.ascx.cs

https://bitbucket.org/kudutest2/kenticogit
C# | 546 lines | 382 code | 76 blank | 88 comment | 71 complexity | 3e76c0901aa7ba8ee15bbde97909612e MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Text;
  6. using System.Web;
  7. using System.Web.UI;
  8. using System.Drawing;
  9. using System.Web.Security;
  10. using CMS.PortalControls;
  11. using CMS.GlobalHelper;
  12. using CMS.TreeEngine;
  13. using CMS.CMSHelper;
  14. using CMS.SiteProvider;
  15. using CMS.EmailEngine;
  16. using CMS.EventLog;
  17. using CMS.DataEngine;
  18. using CMS.WebAnalytics;
  19. using CMS.LicenseProvider;
  20. using CMS.PortalEngine;
  21. using CMS.SettingsProvider;
  22. using CMS.MembershipProvider;
  23. using CMS.URLRewritingEngine;
  24. public partial class CMSWebParts_Membership_Registration_LiveIDUsersRequiredData : CMSAbstractWebPart
  25. {
  26. #region "Private variables"
  27. private WindowsLiveLogin.User liveUser;
  28. private string mDefaultTargetUrl = String.Empty;
  29. #endregion
  30. #region "Public properties"
  31. /// <summary>
  32. /// Gets or sets the value that indicates whether email to user should be sent.
  33. /// </summary>
  34. public bool SendWelcomeEmail
  35. {
  36. get
  37. {
  38. return ValidationHelper.GetBoolean(this.GetValue("SendWelcomeEmail"), true);
  39. }
  40. set
  41. {
  42. this.SetValue("SendWelcomeEmail", value);
  43. }
  44. }
  45. /// <summary>
  46. /// Gets or sets registration approval page URL.
  47. /// </summary>
  48. public string ApprovalPage
  49. {
  50. get
  51. {
  52. return DataHelper.GetNotEmpty(this.GetValue("ApprovalPage"), "");
  53. }
  54. set
  55. {
  56. this.SetValue("ApprovalPage", value);
  57. }
  58. }
  59. /// <summary>
  60. /// Gets or sets the sender email (from).
  61. /// </summary>
  62. public string FromAddress
  63. {
  64. get
  65. {
  66. return DataHelper.GetNotEmpty(this.GetValue("FromAddress"), SettingsKeyProvider.GetStringValue(CMSContext.CurrentSiteName + ".CMSNoreplyEmailAddress"));
  67. }
  68. set
  69. {
  70. this.SetValue("FromAddress", value);
  71. }
  72. }
  73. /// <summary>
  74. /// Gets or sets the recipient email (to).
  75. /// </summary>
  76. public string ToAddress
  77. {
  78. get
  79. {
  80. return DataHelper.GetNotEmpty(this.GetValue("ToAddress"), SettingsKeyProvider.GetStringValue(CMSContext.CurrentSiteName + ".CMSAdminEmailAddress"));
  81. }
  82. set
  83. {
  84. this.SetValue("ToAddress", value);
  85. }
  86. }
  87. /// <summary>
  88. /// Gets or sets the value that indicates whether after successful registration is
  89. /// notification email sent to the administrator
  90. /// </summary>
  91. public bool NotifyAdministrator
  92. {
  93. get
  94. {
  95. return ValidationHelper.GetBoolean(this.GetValue("NotifyAdministrator"), false);
  96. }
  97. set
  98. {
  99. this.SetValue("NotifyAdministrator", value);
  100. }
  101. }
  102. /// <summary>
  103. /// Gets or sets the message which is displayed after successful registration.
  104. /// </summary>
  105. public string DisplayMessage
  106. {
  107. get
  108. {
  109. return ValidationHelper.GetString(this.GetValue("DisplayMessage"), "");
  110. }
  111. set
  112. {
  113. this.SetValue("DisplayMessage", value);
  114. }
  115. }
  116. /// <summary>
  117. /// Gets or sets the value which enables abitity of new user to set password.
  118. /// </summary>
  119. public bool AllowFormsAuthentication
  120. {
  121. get
  122. {
  123. return ValidationHelper.GetBoolean(GetValue("AllowFormsAuthentication"), false);
  124. }
  125. set
  126. {
  127. SetValue("AllowFormsAuthentication", value);
  128. plcPasswordNew.Visible = value;
  129. }
  130. }
  131. /// <summary>
  132. /// Gets or sets the value which enables abitity join liveid with existing account.
  133. /// </summary>
  134. public bool AllowExistingUser
  135. {
  136. get
  137. {
  138. return ValidationHelper.GetBoolean(GetValue("AllowExistingUser"), true);
  139. }
  140. set
  141. {
  142. SetValue("AllowExistingUser", value);
  143. plcPasswordNew.Visible = value;
  144. }
  145. }
  146. /// <summary>
  147. /// Gets or sets the default target url (rediredction when the user is logged in).
  148. /// </summary>
  149. public string DefaultTargetUrl
  150. {
  151. get
  152. {
  153. return ValidationHelper.GetString(GetValue("DefaultTargetUrl"), mDefaultTargetUrl);
  154. }
  155. set
  156. {
  157. SetValue("DefaultTargetUrl", value);
  158. mDefaultTargetUrl = value;
  159. }
  160. }
  161. /// <summary>
  162. /// Gets or sets the value which determines the behaviour if no Live ID user.
  163. /// </summary>
  164. public bool HideForNoLiveID
  165. {
  166. get
  167. {
  168. return ValidationHelper.GetBoolean(GetValue("HideForNoLiveID"), false);
  169. }
  170. set
  171. {
  172. SetValue("HideForNoLiveID", value);
  173. }
  174. }
  175. #endregion
  176. #region "Conversion properties"
  177. /// <summary>
  178. /// Gets or sets the conversion track name used after successful registration.
  179. /// </summary>
  180. public string TrackConversionName
  181. {
  182. get
  183. {
  184. return ValidationHelper.GetString(this.GetValue("TrackConversionName"), SettingsKeyProvider.GetStringValue(CMSContext.CurrentSiteName + ".CMSLiveIDConversionName"));
  185. }
  186. set
  187. {
  188. if ((value != null) && (value.Length > 400))
  189. {
  190. value = value.Substring(0, 400);
  191. }
  192. this.SetValue("TrackConversionName", value);
  193. }
  194. }
  195. /// <summary>
  196. /// Gets or sets the conversion value used after successful registration.
  197. /// </summary>
  198. public double ConversionValue
  199. {
  200. get
  201. {
  202. return ValidationHelper.GetDouble(this.GetValue("ConversionValue"), 0);
  203. }
  204. set
  205. {
  206. this.SetValue("ConversionValue", value);
  207. }
  208. }
  209. #endregion
  210. #region "Methods"
  211. /// <summary>
  212. /// Content loaded event handler.
  213. /// </summary>
  214. public override void OnContentLoaded()
  215. {
  216. base.OnContentLoaded();
  217. SetupControl();
  218. }
  219. /// <summary>
  220. /// Initializes the control properties.
  221. /// </summary>
  222. protected void SetupControl()
  223. {
  224. if (this.StopProcessing)
  225. {
  226. // Do nothing
  227. }
  228. else
  229. {
  230. if (SettingsKeyProvider.GetBoolValue(CMSContext.CurrentSiteName + ".CMSEnableWindowsLiveID"))
  231. {
  232. plcPasswordNew.Visible = this.AllowFormsAuthentication;
  233. pnlExistingUser.Visible = this.AllowExistingUser;
  234. liveUser = SessionHelper.GetValue("windowsliveloginuser") as WindowsLiveLogin.User;
  235. // There is no windows live user object stored in session - hide all
  236. if ((liveUser == null) && HideForNoLiveID)
  237. {
  238. this.Visible = false;
  239. }
  240. // WAI validation
  241. lblPasswordNew.AssociatedControlClientID = passStrength.InputClientID;
  242. }
  243. else
  244. {
  245. // Error label is displayed in Design mode when Windows Live ID is disabled
  246. if (CMSContext.ViewMode == ViewModeEnum.Design)
  247. {
  248. StringBuilder parameter = new StringBuilder();
  249. parameter.Append(GetString("header.sitemanager") + " -> ");
  250. parameter.Append(GetString("settingscategory.cmssettings") + " -> ");
  251. parameter.Append(GetString("settingscategory.cmsmembership") + " -> ");
  252. parameter.Append(GetString("settingscategory.cmsmembershipauthentication") + " -> ");
  253. parameter.Append(GetString("settingscategory.cmswindowsliveid"));
  254. if (CMSContext.CurrentUser.UserSiteManagerAdmin)
  255. {
  256. // Make it link for SiteManager Admin
  257. parameter.Insert(0, "<a href=\"" + URLHelper.GetAbsoluteUrl("~/CMSSiteManager/default.aspx?section=settings") + "\" target=\"_top\">");
  258. parameter.Append("</a>");
  259. }
  260. lblError.Text = String.Format(GetString("mem.liveid.disabled"), parameter.ToString());
  261. plcError.Visible = true;
  262. plcContent.Visible = false;
  263. }
  264. else
  265. {
  266. this.Visible = false;
  267. }
  268. }
  269. }
  270. }
  271. /// <summary>
  272. /// Handles btnOkExist click, joins existing user with liveid token.
  273. /// </summary>
  274. protected void btnOkExist_Click(object sender, EventArgs e)
  275. {
  276. // Live user must be retrieved from session
  277. if (liveUser != null)
  278. {
  279. if (txtUserName.Text != String.Empty)// && (txtPassword.Text != String.Empty))
  280. {
  281. // Try to authenticate user
  282. UserInfo ui = UserInfoProvider.AuthenticateUser(txtUserName.Text, txtPassword.Text, CMSContext.CurrentSiteName);
  283. // Check banned IPs
  284. BannedIPInfoProvider.CheckIPandRedirect(CMSContext.CurrentSiteName, BanControlEnum.Login);
  285. if (ui != null)
  286. {
  287. // Add liveID token to user
  288. ui.UserSettings.WindowsLiveID = liveUser.Id;
  289. UserInfoProvider.SetUserInfo(ui);
  290. // Remove live user object from session, won't be needed
  291. Session.Remove("windowsliveloginuser");
  292. // Set authentication cookie and redirect to page
  293. SetAuthCookieAndRedirect(ui);
  294. }
  295. else // Invalid credentials
  296. {
  297. lblError.Text = GetString("Login_FailureText");
  298. plcError.Visible = true;
  299. }
  300. }
  301. else // User did not fill the form
  302. {
  303. lblError.Text = GetString("mem.liveid.fillloginform");
  304. plcError.Visible = true;
  305. }
  306. }
  307. }
  308. /// <summary>
  309. /// Handles btnOkNew click, creates new user and joins it with liveid token.
  310. /// </summary>
  311. protected void btnOkNew_Click(object sender, EventArgs e)
  312. {
  313. if (liveUser != null)
  314. {
  315. // Validate entered values
  316. string errorMessage = new Validator().IsRegularExp(txtUserNameNew.Text, "^([a-zA-Z0-9_\\-\\.@]+)$", GetString("mem.liveid.fillcorrectusername"))
  317. .IsEmail(txtEmail.Text, GetString("mem.liveid.fillvalidemail")).Result;
  318. string password = passStrength.Text.Trim();
  319. // If password is enabled to set, check it
  320. if (plcPasswordNew.Visible && (errorMessage == String.Empty))
  321. {
  322. if (password == String.Empty)
  323. {
  324. errorMessage = GetString("mem.liveid.specifyyourpass");
  325. }
  326. else if (password != txtConfirmPassword.Text.Trim())
  327. {
  328. errorMessage = GetString("webparts_membership_registrationform.passwordonotmatch");
  329. }
  330. // Check policy
  331. if (!passStrength.IsValid())
  332. {
  333. errorMessage = UserInfoProvider.GetPolicyViolationMessage(CMSContext.CurrentSiteName);
  334. }
  335. }
  336. string siteName = CMSContext.CurrentSiteName;
  337. // Check whether email is unique if it is required
  338. if ((errorMessage == String.Empty) && !UserInfoProvider.IsEmailUnique(txtEmail.Text.Trim(), siteName, 0))
  339. {
  340. errorMessage = GetString("UserInfo.EmailAlreadyExist");
  341. }
  342. // Check reserved names
  343. if ((errorMessage == String.Empty) && UserInfoProvider.NameIsReserved(siteName, txtUserNameNew.Text.Trim()))
  344. {
  345. errorMessage = GetString("Webparts_Membership_RegistrationForm.UserNameReserved").Replace("%%name%%", HTMLHelper.HTMLEncode(txtUserNameNew.Text.Trim()));
  346. }
  347. if (errorMessage == String.Empty)
  348. {
  349. string userName = txtUserNameNew.Text.Trim();
  350. // Check if user with given username already exists
  351. UserInfo ui = UserInfoProvider.GetUserInfo(userName);
  352. UserInfo siteui = UserInfoProvider.GetUserInfo(UserInfoProvider.EnsureSitePrefixUserName(userName, CMSContext.CurrentSite));
  353. // User with given username is already registered
  354. if ((ui != null) || (siteui != null))
  355. {
  356. plcError.Visible = true;
  357. lblError.Text = GetString("mem.openid.usernameregistered");
  358. }
  359. else
  360. {
  361. // Register new user
  362. string error = this.DisplayMessage;
  363. ui = UserInfoProvider.AuthenticateWindowsLiveUser(liveUser.Id, siteName, false, ref error);
  364. this.DisplayMessage = error;
  365. if (ui != null)
  366. {
  367. // Set additional information
  368. ui.UserName = ui.UserNickName = userName;
  369. // Ensure site prefixes
  370. if (UserInfoProvider.UserNameSitePrefixEnabled(siteName))
  371. {
  372. ui.UserName = UserInfoProvider.EnsureSitePrefixUserName(userName, CMSContext.CurrentSite);
  373. }
  374. ui.Email = txtEmail.Text;
  375. // Set password
  376. if (plcPasswordNew.Visible)
  377. {
  378. UserInfoProvider.SetPassword(ui, password);
  379. // If user can choose password then is not considered external(external user can't login in common way)
  380. ui.IsExternal = false;
  381. }
  382. UserInfoProvider.SetUserInfo(ui);
  383. // Remove live user object from session, won't be needed
  384. Session.Remove("windowsliveloginuser");
  385. // Send registration e-mails
  386. UserInfoProvider.SendRegistrationEmails(ui, this.ApprovalPage, password, true, this.SendWelcomeEmail);
  387. // Notify administrator
  388. bool requiresConfirmation = SettingsKeyProvider.GetBoolValue(siteName + ".CMSRegistrationEmailConfirmation");
  389. if (!requiresConfirmation && this.NotifyAdministrator && (this.FromAddress != String.Empty) && (this.ToAddress != String.Empty))
  390. {
  391. UserInfoProvider.NotifyAdministrator(ui, this.FromAddress, this.ToAddress);
  392. }
  393. // Track registration into analytics
  394. UserInfoProvider.TrackUserRegistration(this.TrackConversionName, this.ConversionValue, siteName, ui);
  395. // Log registration activity
  396. if ((CMSContext.ViewMode == ViewModeEnum.LiveSite) && ActivitySettingsHelper.ActivitiesEnabledAndModuleLoaded(siteName) && ActivitySettingsHelper.ActivitiesEnabledForThisUser(CMSContext.CurrentUser)
  397. && ActivitySettingsHelper.UserRegistrationEnabled(siteName))
  398. {
  399. int contactId = ModuleCommands.OnlineMarketingGetUserLoginContactID(ui);
  400. ModuleCommands.OnlineMarketingUpdateContactFromExternalData(ui, contactId);
  401. TreeNode currentDoc = CMSContext.CurrentDocument;
  402. ActivityLogProvider.LogRegistrationActivity(contactId,
  403. ui, URLHelper.CurrentRelativePath, (currentDoc != null ? currentDoc.NodeID : 0), siteName, CMSContext.Campaign, (currentDoc != null ? currentDoc.DocumentCulture : null));
  404. }
  405. // Set authentication cookie and redirect to page
  406. SetAuthCookieAndRedirect(ui);
  407. // Display error message
  408. if (!String.IsNullOrEmpty(this.DisplayMessage))
  409. {
  410. lblInfo.Visible = true;
  411. lblInfo.Text = this.DisplayMessage;
  412. plcForm.Visible = false;
  413. }
  414. else
  415. {
  416. URLHelper.Redirect(ResolveUrl("~/Default.aspx"));
  417. }
  418. }
  419. }
  420. }
  421. else
  422. {
  423. lblError.Text = errorMessage;
  424. plcError.Visible = true;
  425. }
  426. }
  427. }
  428. /// <summary>
  429. /// Helper method, set authentication cookie and redirect to return URL or default page.
  430. /// </summary>
  431. /// <param name="ui">User info</param>
  432. private void SetAuthCookieAndRedirect(UserInfo ui)
  433. {
  434. // Create autentification cookie
  435. if (ui.Enabled)
  436. {
  437. UserInfoProvider.SetAuthCookieWithUserData(ui.UserName, true, Session.Timeout, new string[] { "liveidlogin" });
  438. // Log activity
  439. string siteName = CMSContext.CurrentSiteName;
  440. if ((CMSContext.ViewMode == ViewModeEnum.LiveSite) && ActivitySettingsHelper.ActivitiesEnabledAndModuleLoaded(siteName) && ActivitySettingsHelper.UserLoginEnabled(siteName))
  441. {
  442. int contactId = ModuleCommands.OnlineMarketingGetUserLoginContactID(ui);
  443. ActivityLogHelper.UpdateContactLastLogon(contactId);
  444. if (ActivitySettingsHelper.ActivitiesEnabledForThisUser(ui))
  445. {
  446. TreeNode currentDoc = CMSContext.CurrentDocument;
  447. ActivityLogProvider.LogLoginActivity(contactId,
  448. ui, URLHelper.CurrentRelativePath, currentDoc.NodeID, siteName, CMSContext.Campaign, currentDoc.DocumentCulture);
  449. }
  450. }
  451. // Redirect to default page
  452. if (!String.IsNullOrEmpty(this.DefaultTargetUrl))
  453. {
  454. URLHelper.Redirect(ResolveUrl(this.DefaultTargetUrl));
  455. }
  456. // If there is some return page redirect there
  457. else if ((liveUser != null) && !string.IsNullOrEmpty(liveUser.Context))
  458. {
  459. URLHelper.Redirect(liveUser.Context);
  460. }
  461. // Refresh current page to update see user signed in
  462. else
  463. {
  464. string url = URLRewriter.CurrentURL;
  465. URLHelper.Redirect(url);
  466. }
  467. }
  468. }
  469. #endregion
  470. }