-
Notifications
You must be signed in to change notification settings - Fork 11
/
OAuthProvider.vb
56 lines (54 loc) · 2.23 KB
/
OAuthProvider.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Imports System
Imports System.Web
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Security
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.ExpressApp.Web
Imports Microsoft.AspNet.Identity.Owin
Imports Microsoft.Owin.Security
Imports MySolution.Module.BusinessObjects
Imports MySolution.Module.Web.Security
Namespace MySolution.Web.Security
Public Class OAuthProvider
Implements IAuthenticationProvider
Private ReadOnly userType As Type
Private ReadOnly security As SecurityStrategyComplex
Public Property CreateUserAutomatically() As Boolean
Public Sub New(ByVal userType As Type, ByVal security As SecurityStrategyComplex)
Guard.ArgumentNotNull(userType, "userType")
Me.userType = userType
Me.security = security
End Sub
Public Function Authenticate(ByVal objectSpace As IObjectSpace) As Object Implements DevExpress.ExpressApp.Security.IAuthenticationProviderV2.Authenticate
Dim user As ApplicationUser = Nothing
Dim externalLoginInfo As ExternalLoginInfo = Authenticate()
If externalLoginInfo IsNot Nothing Then
Dim userEmail As String = externalLoginInfo.Email
If userEmail IsNot Nothing Then
user = DirectCast(objectSpace.FindObject(userType, CriteriaOperator.Parse("OAuthAuthenticationEmails[Email = ?]", userEmail)), ApplicationUser)
If user Is Nothing AndAlso CreateUserAutomatically Then
user = DirectCast(objectSpace.CreateObject(userType), ApplicationUser)
user.UserName = userEmail
Dim email As EmailEntity = objectSpace.CreateObject(Of EmailEntity)()
email.Email = userEmail
user.OAuthAuthenticationEmails.Add(email)
CType(security, CustomSecurityStrategyComplex).InitializeNewUser(objectSpace, user)
objectSpace.CommitChanges()
End If
End If
Else
WebApplication.Redirect(WebApplication.LogonPage)
End If
If user Is Nothing Then
Throw New Exception("Login failed")
End If
Return user
End Function
Private Function Authenticate() As ExternalLoginInfo
Return HttpContext.Current.GetOwinContext().Authentication.GetExternalLoginInfo()
End Function
Public Sub Setup(ParamArray ByVal args() As Object) Implements IAuthenticationProvider.Setup
End Sub
End Class
End Namespace