PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/WP7.1/Templates/VB/WPCloud.Mem/WindowsPhoneCloud.Web/Controllers/AccountController.vb

#
Visual Basic | 143 lines | 97 code | 26 blank | 20 comment | 1 complexity | e0d2b4130910b98342a5870970f4ec5b MD5 | raw file
  1. ' ----------------------------------------------------------------------------------
  2. ' Microsoft Developer & Platform Evangelism
  3. '
  4. ' Copyright (c) Microsoft Corporation. All rights reserved.
  5. '
  6. ' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  7. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
  8. ' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  9. ' ----------------------------------------------------------------------------------
  10. ' The example companies, organizations, products, domain names,
  11. ' e-mail addresses, logos, people, places, and events depicted
  12. ' herein are fictitious. No association with any real company,
  13. ' organization, product, domain name, email address, logo, person,
  14. ' places, or events is intended or should be inferred.
  15. ' ----------------------------------------------------------------------------------
  16. Imports Microsoft.Samples.WindowsPhoneCloud.Web.UserAccountWrappers
  17. Imports Microsoft.Samples.WindowsPhoneCloud.Web.Models
  18. Imports Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure
  19. Imports System.Security.Principal
  20. Imports System.Globalization
  21. Namespace Controllers
  22. <HandleError()>
  23. Public Class AccountController
  24. Inherits System.Web.Mvc.Controller
  25. Private ReadOnly userPrivilegesRepository As IUserPrivilegesRepository
  26. Public Sub New()
  27. Me.New(New UserTablesServiceContext())
  28. End Sub
  29. <CLSCompliant(False)>
  30. Public Sub New(ByVal userPrivilegesRepository As IUserPrivilegesRepository)
  31. Me.userPrivilegesRepository = userPrivilegesRepository
  32. End Sub
  33. Public Function Unauthorized() As ActionResult
  34. Return Me.View()
  35. End Function
  36. Public Function LogOn() As ActionResult
  37. Return Me.View(New LogOnModel())
  38. End Function
  39. <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", Justification:="Needs to take same parameter type as Controller.Redirect()"), HttpPost(), ValidateAntiForgeryToken()> _
  40. Public Function LogOn(ByVal model As LogOnModel, ByVal returnUrl As String) As ActionResult
  41. If ModelState.IsValid Then
  42. If Membership.ValidateUser(model.UserName, model.Password) Then
  43. FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe)
  44. If Url.IsLocalUrl(returnUrl) AndAlso returnUrl.Length > 1 AndAlso returnUrl.StartsWith("/") _
  45. AndAlso Not returnUrl.StartsWith("//") AndAlso Not returnUrl.StartsWith("/\\") Then
  46. Return Redirect(returnUrl)
  47. Else
  48. Return RedirectToAction("Index", "Home")
  49. End If
  50. Else
  51. ModelState.AddModelError("", "The user name or password provided is incorrect.")
  52. End If
  53. End If
  54. ' If we got this far, something failed, redisplay form
  55. Return View(model)
  56. End Function
  57. Public Function LogOff() As ActionResult
  58. FormsAuthentication.SignOut()
  59. Return RedirectToAction("Index", "Home")
  60. End Function
  61. #If MEMBERSHIP Then
  62. Public Function Register() As ActionResult
  63. Return View()
  64. End Function
  65. <HttpPost(), ValidateAntiForgeryToken()> _
  66. Public Function Register(ByVal model As RegisterModel) As ActionResult
  67. If ModelState.IsValid Then
  68. ' Attempt to register the user
  69. Dim createStatus As MembershipCreateStatus
  70. Membership.CreateUser(model.UserName, model.Password, model.Email, Nothing, Nothing, True, Nothing, createStatus)
  71. If createStatus = MembershipCreateStatus.Success Then
  72. FormsAuthentication.SetAuthCookie(model.UserName, False) ' createPersistentCookie
  73. Me.SetUserDefaultUserPrivileges(Membership.GetUser(model.UserName).ProviderUserKey.ToString())
  74. Return RedirectToAction("Index", "Home")
  75. Else
  76. ModelState.AddModelError(String.Empty, ErrorCodeToString(createStatus))
  77. End If
  78. End If
  79. ' If we got this far, something failed, redisplay form
  80. Return View(model)
  81. End Function
  82. Private Shared Function ErrorCodeToString(ByVal createStatus As MembershipCreateStatus) As String
  83. ' See http://go.microsoft.com/fwlink/?LinkID=177550 for
  84. ' a full list of status codes.
  85. Select Case createStatus
  86. Case MembershipCreateStatus.DuplicateUserName
  87. Return "User name already exists. Please enter a different user name."
  88. Case MembershipCreateStatus.DuplicateEmail
  89. Return "A user name for that e-mail address already exists. Please enter a different e-mail address."
  90. Case MembershipCreateStatus.InvalidPassword
  91. Return "The password provided is invalid. Please enter a valid password value."
  92. Case MembershipCreateStatus.InvalidEmail
  93. Return "The e-mail address provided is invalid. Please check the value and try again."
  94. Case MembershipCreateStatus.InvalidAnswer
  95. Return "The password retrieval answer provided is invalid. Please check the value and try again."
  96. Case MembershipCreateStatus.InvalidQuestion
  97. Return "The password retrieval question provided is invalid. Please check the value and try again."
  98. Case MembershipCreateStatus.InvalidUserName
  99. Return "The user name provided is invalid. Please check the value and try again."
  100. Case MembershipCreateStatus.ProviderError
  101. Return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator."
  102. Case MembershipCreateStatus.UserRejected
  103. Return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator."
  104. Case Else
  105. Return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator."
  106. End Select
  107. End Function
  108. Private Sub SetUserDefaultUserPrivileges(ByVal userId As String)
  109. Me.userPrivilegesRepository.AddPrivilegeToUser(userId, PrivilegeConstants.TablesUsagePrivilege)
  110. Me.userPrivilegesRepository.AddPrivilegeToUser(userId, PrivilegeConstants.BlobsUsagePrivilege)
  111. Me.userPrivilegesRepository.AddPrivilegeToUser(userId, PrivilegeConstants.QueuesUsagePrivilege)
  112. Me.userPrivilegesRepository.AddPrivilegeToUser(userId, PrivilegeConstants.SqlUsagePrivilege)
  113. Me.userPrivilegesRepository.AddPrivilegeToUser(userId, String.Format(CultureInfo.InvariantCulture, "{0}{1}", "SampleData", PrivilegeConstants.TablePrivilegeSuffix))
  114. End Sub
  115. #End If
  116. End Class
  117. End Namespace