PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

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

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