Skip to content

Instantly share code, notes, and snippets.

@byrnedo
Created January 11, 2019 12:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save byrnedo/d8ca5611f89145ab26b6dfce4826d693 to your computer and use it in GitHub Desktop.
Save byrnedo/d8ca5611f89145ab26b6dfce4826d693 to your computer and use it in GitHub Desktop.
VB Dotnet Owin Auth0 Startup
Imports Microsoft.Owin
Imports Microsoft.Owin.Security.OpenIdConnect
Imports Owin
Imports Microsoft.Owin.Security
Imports Microsoft.Owin.Security.Notifications
Imports Microsoft.Owin.Security.Cookies
Imports Microsoft.IdentityModel.Protocols.OpenIdConnect
Imports Microsoft.IdentityModel.Tokens
Imports System.Threading.Tasks
<Assembly: OwinStartup(GetType(App_Start.Startup))>
Namespace App_Start
Public Class Startup
Public Sub Configuration(app As IAppBuilder)
' Configure Auth0 parameters
Dim auth0Conf = ConfigurationManager.GetSection("auth0")
Dim domain = auth0Conf("Domain")
Dim clientID = auth0Conf("ClientID")
Dim clientSecret = auth0Conf("ClientSecret")
Dim redirectURI = auth0Conf("RedirectURI")
Dim postLogoutRedirectURI = auth0Conf("PostLogoutRedirectURI")
Dim redirectCB = Function(notification As RedirectToIdentityProviderNotification(Of OpenIdConnectMessage, OpenIdConnectAuthenticationOptions))
If (notification.ProtocolMessage.RequestType = OpenIdConnectRequestType.Logout) Then
Dim logoutUri = $"https://{domain}/v2/logout?client_id={clientID}"
Dim postLogoutUri = notification.ProtocolMessage.PostLogoutRedirectUri
If (Not String.IsNullOrEmpty(postLogoutUri)) Then
If (postLogoutUri.StartsWith("/")) Then
' transform to absolute
Dim request = notification.Request
postLogoutUri = request.Scheme + "://" + request.Host.ToString() + request.PathBase.ToString() + postLogoutUri
End If
logoutUri += $"&returnTo={ Uri.EscapeDataString(postLogoutUri)}"
End If
notification.Response.Redirect(logoutUri)
notification.HandleResponse()
End If
Return Task.FromResult(0)
End Function
' Enable the Cookie saver middleware to work around a bug in the OWIN implementation
app.UseKentorOwinCookieSaver()
' Set Cookies as default authentication type
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType)
app.UseCookieAuthentication(New CookieAuthenticationOptions With
{
.AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
.LoginPath = New PathString("/Account/Login")
})
' Configure Auth0 authentication
app.UseOpenIdConnectAuthentication(New OpenIdConnectAuthenticationOptions With
{
.AuthenticationType = "Auth0",
.Authority = $"https://{domain}",
.ClientId = clientID,
.ClientSecret = clientSecret,
.RedirectUri = redirectURI,
.PostLogoutRedirectUri = postLogoutRedirectURI,
.ResponseType = OpenIdConnectResponseType.CodeIdToken,
.Scope = "openid profile",
.TokenValidationParameters = New TokenValidationParameters With
{
.NameClaimType = "name"
},
.Notifications = New OpenIdConnectAuthenticationNotifications With
{
.RedirectToIdentityProvider = redirectCB
}
})
End Sub
End Class
End Namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment