ndamgaard
/
XAF_how-to-use-google-facebook-and-microsoft-accounts-in-aspnet-xaf-applications-oauth2-demo-t535280
Public
forked from dennis-garavsky/XAF_how-to-use-google-facebook-and-microsoft-accounts-in-aspnet-xaf-applications-oauth2-demo-t535280
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.vb
58 lines (55 loc) · 3.97 KB
/
Startup.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
57
58
Imports Microsoft.VisualBasic
Imports System
Imports System.Configuration
Imports System.Threading.Tasks
Imports Microsoft.Owin
Imports Microsoft.Owin.Security
Imports Microsoft.Owin.Security.Cookies
Imports Microsoft.Owin.Security.Facebook
Imports Microsoft.Owin.Security.Google
Imports Owin
Imports System.Net.Http
Imports System.Threading
Imports Microsoft.Owin.Security.MicrosoftAccount
Imports DevExpress.ExpressApp.Web
Imports System.Security.Claims
<Assembly: OwinStartup(GetType(AuthenticationOwin.Web.Startup))>
Namespace AuthenticationOwin.Web
Public Class Startup
Private Shared googleClientID As String = ConfigurationManager.AppSettings("GoogleClientID")
Private Shared googleClientSecret As String = ConfigurationManager.AppSettings("GoogleClientSecret")
Private Shared facebookClientID As String = ConfigurationManager.AppSettings("FacebookClientID")
Private Shared facebookClientSecret As String = ConfigurationManager.AppSettings("FacebookClientSecret")
Private Shared microsoftClientID As String = ConfigurationManager.AppSettings("MicrosoftClientID")
Private Shared microsoftClientSecret As String = ConfigurationManager.AppSettings("MicrosoftClientSecret")
Public Sub Configuration(ByVal app As IAppBuilder)
app.SetDefaultSignInAsAuthenticationType("External")
'app.UseCors(CorsOptions.AllowAll);
app.UseCookieAuthentication(New CookieAuthenticationOptions With {
.AuthenticationType = "External",
.AuthenticationMode = AuthenticationMode.Passive,
.CookieName = ".AspNet.External",
.ExpireTimeSpan = TimeSpan.FromMinutes(5)})
If (Not String.IsNullOrEmpty(googleClientID)) AndAlso (Not String.IsNullOrEmpty(googleClientID)) Then
app.UseGoogleAuthentication(New GoogleOAuth2AuthenticationOptions() With {.ClientId = googleClientID, .ClientSecret = googleClientSecret})
End If
If (Not String.IsNullOrEmpty(facebookClientID)) AndAlso (Not String.IsNullOrEmpty(facebookClientSecret)) Then
Dim facebookAuthOptions As FacebookAuthenticationOptions = New FacebookAuthenticationOptions With {.AppId = facebookClientID, .AppSecret = facebookClientSecret, .UserInformationEndpoint = "https://graph.facebook.com/v2.4/me?fields=id,name,email"}
app.UseFacebookAuthentication(facebookAuthOptions)
End If
If ((Not String.IsNullOrEmpty(microsoftClientID)) AndAlso (Not String.IsNullOrEmpty(microsoftClientSecret))) Then
Dim microsoftAccountAuthenticationOptions As MicrosoftAccountAuthenticationOptions = New MicrosoftAccountAuthenticationOptions With {
.ClientId = microsoftClientID,
.ClientSecret = microsoftClientSecret,
.Provider = New MicrosoftAccountAuthenticationProvider() With {.OnAuthenticated = Function(context)
Dim email = context.User("userPrincipalName")
If (email IsNot Nothing) Then
context.Identity.AddClaim(New Claim(ClaimTypes.Email, email.ToString()))
End If
Return Task.FromResult(0)
End Function}}
app.UseMicrosoftAccountAuthentication(microsoftAccountAuthenticationOptions)
End If
End Sub
End Class
End Namespace