PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/WP7.1/Templates/VB/WPCloud.Mem/WindowsPhoneCloud.Web/Services/AuthenticationService.vb

#
Visual Basic | 107 lines | 74 code | 18 blank | 15 comment | 0 complexity | 2f9a72149ab7fe0e2cb9542b369bf667 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 System.Globalization
  17. Imports System.Net
  18. Imports System.ServiceModel
  19. Imports System.ServiceModel.Activation
  20. Imports System.ServiceModel.Web
  21. Imports Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure
  22. Imports Microsoft.Samples.WindowsPhoneCloud.Web.Models
  23. Imports Microsoft.Samples.WindowsPhoneCloud.Web.UserAccountWrappers
  24. Namespace Services
  25. <ServiceBehavior(IncludeExceptionDetailInFaults:=False), AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
  26. Public Class AuthenticationService
  27. Implements IAuthenticationService
  28. Private ReadOnly formsAuth As IFormsAuthentication
  29. Private ReadOnly membershipService As IMembershipService
  30. Private ReadOnly userPrivilegesRepository As IUserPrivilegesRepository
  31. Public Sub New()
  32. Me.New(New FormsAuthenticationService(), New AccountMembershipService(), New UserTablesServiceContext())
  33. End Sub
  34. <CLSCompliant(False)>
  35. Public Sub New(ByVal formsAuth As IFormsAuthentication, ByVal membershipService As IMembershipService, ByVal userPrivilegesRepository As IUserPrivilegesRepository)
  36. If formsAuth Is Nothing Then
  37. Throw New ArgumentNullException("formsAuth", "The Forms Authentication service cannot be null.")
  38. End If
  39. If membershipService Is Nothing Then
  40. Throw New ArgumentNullException("membershipService", "The Membership service cannot be null.")
  41. End If
  42. If userPrivilegesRepository Is Nothing Then
  43. Throw New ArgumentNullException("userPrivilegesRepository", "The User Privileges Repository cannot be null.")
  44. End If
  45. Me.formsAuth = formsAuth
  46. Me.membershipService = membershipService
  47. Me.userPrivilegesRepository = userPrivilegesRepository
  48. End Sub
  49. Public Function GenerateAuthToken(ByVal login As Login) As String Implements IAuthenticationService.GenerateAuthToken
  50. If (login Is Nothing) OrElse String.IsNullOrEmpty(login.UserName) OrElse String.IsNullOrEmpty(login.Password) Then
  51. Throw New WebFaultException(Of String)("Invalid credentials.", HttpStatusCode.BadRequest)
  52. End If
  53. If Me.membershipService.ValidateUser(login.UserName, login.Password) Then
  54. Dim user = Me.membershipService.GetUser(login.UserName)
  55. Dim ticket = New FormsAuthenticationTicket(user.UserName, False, Integer.MaxValue)
  56. Dim token = Me.formsAuth.Encrypt(ticket)
  57. Return token
  58. End If
  59. Return String.Empty
  60. End Function
  61. Public Function ValidateAuthToken(ByVal token As String) As String Implements IAuthenticationService.ValidateAuthToken
  62. If String.IsNullOrEmpty(token) Then
  63. Throw New WebFaultException(Of String)("Token cannot be null or empty.", HttpStatusCode.BadRequest)
  64. End If
  65. Dim ticket = Me.formsAuth.Decrypt(token)
  66. If ticket IsNot Nothing Then
  67. Return ticket.Name
  68. End If
  69. Return Nothing
  70. End Function
  71. Public Function CreateUser(ByVal user As RegistrationUser) As String Implements IAuthenticationService.CreateUser
  72. If (user Is Nothing) OrElse String.IsNullOrEmpty(user.Name) OrElse String.IsNullOrEmpty(user.EMail) OrElse String.IsNullOrEmpty(user.Password) Then
  73. Throw New WebFaultException(Of String)("Invalid user information.", HttpStatusCode.BadRequest)
  74. End If
  75. Dim createStatus = Me.membershipService.CreateUser(user.Name, user.Password, user.EMail)
  76. If createStatus.Equals(MembershipCreateStatus.Success) Then
  77. Me.SetUserDefaultPermissions(Me.membershipService.GetUser(user.Name).ProviderUserKey.ToString())
  78. End If
  79. Return createStatus.ToString()
  80. End Function
  81. Private Sub SetUserDefaultPermissions(ByVal userId As String)
  82. Me.userPrivilegesRepository.AddPrivilegeToUser(userId, PrivilegeConstants.TablesUsagePrivilege)
  83. Me.userPrivilegesRepository.AddPrivilegeToUser(userId, PrivilegeConstants.BlobsUsagePrivilege)
  84. Me.userPrivilegesRepository.AddPrivilegeToUser(userId, PrivilegeConstants.QueuesUsagePrivilege)
  85. Me.userPrivilegesRepository.AddPrivilegeToUser(userId, PrivilegeConstants.SqlUsagePrivilege)
  86. Me.userPrivilegesRepository.AddPrivilegeToUser(userId, String.Format(CultureInfo.InvariantCulture, "{0}{1}", "SampleData", PrivilegeConstants.TablePrivilegeSuffix))
  87. End Sub
  88. End Class
  89. End Namespace