PageRenderTime 56ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/MyVMPortal/Account/Manage.aspx.cs

https://github.com/mwasham/wasvcmgmntapi
C# | 104 lines | 78 code | 19 blank | 7 comment | 9 complexity | 6934b9c35a2be3ba06b5b6382484cd0d MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web.UI.WebControls;
  5. using Microsoft.AspNet.Membership.OpenAuth;
  6. namespace MyVMPortal.Account
  7. {
  8. public partial class Manage : System.Web.UI.Page
  9. {
  10. protected string SuccessMessage
  11. {
  12. get;
  13. private set;
  14. }
  15. protected bool CanRemoveExternalLogins
  16. {
  17. get;
  18. private set;
  19. }
  20. protected void Page_Load()
  21. {
  22. if (!IsPostBack)
  23. {
  24. // Determine the sections to render
  25. var hasLocalPassword = OpenAuth.HasLocalPassword(User.Identity.Name);
  26. setPassword.Visible = !hasLocalPassword;
  27. changePassword.Visible = hasLocalPassword;
  28. CanRemoveExternalLogins = hasLocalPassword;
  29. // Render success message
  30. var message = Request.QueryString["m"];
  31. if (message != null)
  32. {
  33. // Strip the query string from action
  34. Form.Action = ResolveUrl("~/Account/Manage.aspx");
  35. SuccessMessage =
  36. message == "ChangePwdSuccess" ? "Your password has been changed."
  37. : message == "SetPwdSuccess" ? "Your password has been set."
  38. : message == "RemoveLoginSuccess" ? "The external login was removed."
  39. : String.Empty;
  40. successMessage.Visible = !String.IsNullOrEmpty(SuccessMessage);
  41. }
  42. }
  43. // Data-bind the list of external accounts
  44. var accounts = OpenAuth.GetAccountsForUser(User.Identity.Name);
  45. CanRemoveExternalLogins = CanRemoveExternalLogins || accounts.Count() > 1;
  46. externalLoginsList.DataSource = accounts;
  47. externalLoginsList.DataBind();
  48. }
  49. protected void setPassword_Click(object sender, EventArgs e)
  50. {
  51. if (IsValid)
  52. {
  53. var result = OpenAuth.AddLocalPassword(User.Identity.Name, password.Text);
  54. if (result.IsSuccessful)
  55. {
  56. Response.Redirect("~/Account/Manage.aspx?m=SetPwdSuccess");
  57. }
  58. else
  59. {
  60. newPasswordMessage.Text = result.ErrorMessage;
  61. }
  62. }
  63. }
  64. protected void externalLoginsList_ItemDeleting(object sender, ListViewDeleteEventArgs e)
  65. {
  66. var providerName = (string)e.Keys["ProviderName"];
  67. var providerUserId = (string)e.Keys["ProviderUserId"];
  68. var m = OpenAuth.DeleteAccount(User.Identity.Name, providerName, providerUserId)
  69. ? "?m=RemoveLoginSuccess"
  70. : String.Empty;
  71. Response.Redirect("~/Account/Manage.aspx" + m);
  72. }
  73. protected T Item<T>() where T : class
  74. {
  75. return GetDataItem() as T ?? default(T);
  76. }
  77. protected static string ConvertToDisplayDateTime(DateTime? utcDateTime)
  78. {
  79. // You can change this method to convert the UTC date time into the desired display
  80. // offset and format. Here we're converting it to the server timezone and formatting
  81. // as a short date and a long time string, using the current thread culture.
  82. return utcDateTime.HasValue ? utcDateTime.Value.ToLocalTime().ToString("G") : "[never]";
  83. }
  84. }
  85. }