PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Visual Basic | 121 lines | 83 code | 16 blank | 22 comment | 1 complexity | 7625c5f6a988280bf714394f40eb8e84 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.Net
  20. Imports System.Linq.Expressions
  21. Imports System.Data.Services.Common
  22. Imports System.Data.Services
  23. Imports System.Data.Objects
  24. Imports System.Data.Entity.Infrastructure
  25. Namespace Services
  26. ' Summary:
  27. ' Sample OData Service exposing SQL Azure data.
  28. ' This sample service provides read-acess to all the collections of entities of the SQL Azure sample context,
  29. ' and validates the user credentials
  30. Public Class SqlAzureSampleODataService
  31. Inherits DataService(Of ObjectContext)
  32. #If ACS Then
  33. Private ReadOnly Property UserId() As String
  34. Get
  35. Dim identity = TryCast(HttpContext.Current.User.Identity, Microsoft.IdentityModel.Claims.IClaimsIdentity)
  36. Return identity.Claims.Single(Function(c) c.ClaimType = Microsoft.IdentityModel.Claims.ClaimTypes.NameIdentifier).Value
  37. End Get
  38. End Property
  39. #Else
  40. Private ReadOnly context As HttpContextBase
  41. Private ReadOnly formsAuth As IFormsAuthentication
  42. Private ReadOnly membershipService As IMembershipService
  43. Private ReadOnly userPrivilegesRepository As IUserPrivilegesRepository
  44. Public Sub New()
  45. Me.New(New HttpContextWrapper(HttpContext.Current), New SqlDataContext(), New FormsAuthenticationService(), New AccountMembershipService())
  46. End Sub
  47. <CLSCompliant(False)> _
  48. Public Sub New(ByVal context As HttpContextBase, ByVal userPrivilegesRepository As IUserPrivilegesRepository, ByVal formsAuth As IFormsAuthentication, ByVal membershipService As IMembershipService)
  49. If (context Is Nothing) AndAlso (HttpContext.Current Is Nothing) Then
  50. Throw New ArgumentNullException("context", "The context cannot be null if not running on a Web context.")
  51. End If
  52. Me.context = context
  53. Me.userPrivilegesRepository = userPrivilegesRepository
  54. Me.formsAuth = formsAuth
  55. Me.membershipService = membershipService
  56. End Sub
  57. Private ReadOnly Property UserId() As String
  58. Get
  59. Dim ticketValue As String = Nothing
  60. Dim cookie = Me.context.Request.Cookies(Me.formsAuth.FormsCookieName)
  61. If cookie IsNot Nothing Then
  62. ' From cookie.
  63. ticketValue = cookie.Value
  64. ElseIf Me.context.Request.Headers("AuthToken") IsNot Nothing Then
  65. ' From HTTP header.
  66. ticketValue = Me.context.Request.Headers("AuthToken")
  67. End If
  68. If (Not String.IsNullOrEmpty(ticketValue)) Then
  69. Dim ticket As FormsAuthenticationTicket
  70. Try
  71. ticket = Me.formsAuth.Decrypt(ticketValue)
  72. Catch
  73. Throw New DataServiceException(CInt(Fix(HttpStatusCode.Unauthorized)), "The authorization ticket cannot be decrypted.")
  74. End Try
  75. If ticket IsNot Nothing Then
  76. ' Authorize Sql Azure OData Service usage.
  77. Dim auxiliarUserId = Me.membershipService.GetUser(New FormsIdentity(ticket).Name).ProviderUserKey.ToString()
  78. If (Not Me.userPrivilegesRepository.HasUserPrivilege(auxiliarUserId, PrivilegeConstants.SqlUsagePrivilege)) Then
  79. Throw New DataServiceException(CInt(Fix(HttpStatusCode.Unauthorized)), "You have no permission to use SQL Azure.")
  80. End If
  81. Return auxiliarUserId
  82. Else
  83. Throw New DataServiceException(CInt(Fix(HttpStatusCode.Unauthorized)), "The authorization token is no longer valid.")
  84. End If
  85. Else
  86. Throw New DataServiceException(CInt(Fix(HttpStatusCode.NotFound)), "Resource not found.")
  87. End If
  88. End Get
  89. End Property
  90. #End If
  91. Public Shared Sub InitializeService(ByVal config As DataServiceConfiguration)
  92. config.SetEntitySetAccessRule("SqlSampleData", EntitySetRights.AllRead)
  93. config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2
  94. End Sub
  95. <QueryInterceptor("SqlSampleData")> _
  96. Public Function QuerySqlSampleData() As Expression(Of Func(Of SqlSampleData, Boolean))
  97. Return Function(c) c.IsPublic OrElse c.UserId.Equals(Me.UserId, StringComparison.OrdinalIgnoreCase)
  98. End Function
  99. <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification:="The instance created of the SqlSampleDataContext class is used as the data source of the DataService class.")>
  100. Protected Overrides Function CreateDataSource() As ObjectContext
  101. Dim sampleDataContext = TryCast(New SqlDataContext(), IObjectContextAdapter)
  102. Return sampleDataContext.ObjectContext
  103. End Function
  104. End Class
  105. End Namespace