Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/KeycloakIdentityModel/KeycloakIdentityModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Compile Include="Models\Configuration\IKeycloakSettings.cs" />
<Compile Include="Extensions\DateTimeExtension.cs" />
<Compile Include="Extensions\JwtSecurityTokenExtension.cs" />
<Compile Include="Models\EventArgs\OnAuthenticatedEventArgs.cs" />
<Compile Include="Utilities\ClaimMapping\ClaimLookup.cs" />
<Compile Include="Utilities\ClaimMapping\ClaimMappings.cs" />
<Compile Include="Global.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace KeycloakIdentityModel.Models.EventArgs
{
public class OnAuthenticatedEventArgs: System.EventArgs
{
public string RedirectUri { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using KeycloakIdentityModel.Models.Configuration;
using KeycloakIdentityModel.Models.EventArgs;
using Microsoft.Owin;
using Microsoft.Owin.Security;

namespace Owin.Security.Keycloak
Expand Down Expand Up @@ -191,5 +193,10 @@ public KeycloakAuthenticationOptions()

public string CallbackPath { get; set; }
public string ResponseType { get; set; }

/// <summary>
/// OPTIONAL: Triggers <see cref="Action"/> to fire after a successful authentication.
/// </summary>
public Action<IOwinContext, OnAuthenticatedEventArgs> OnAuthenticated { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Security.Claims;
using System.Threading.Tasks;
using KeycloakIdentityModel;
using KeycloakIdentityModel.Models.EventArgs;
using KeycloakIdentityModel.Models.Responses;
using Microsoft.Owin;
using Microsoft.Owin.Security;
Expand Down Expand Up @@ -79,14 +80,19 @@ stateData[Constants.CacheTypes.AuthenticationProperties] as AuthenticationProper
var kcIdentity =
await KeycloakIdentity.ConvertFromAuthResponseAsync(Options, authResult, Request.Uri);
var identity = await kcIdentity.ToClaimsIdentityAsync();
Context.Authentication.User.AddIdentity(identity);

Context.Authentication.User = new ClaimsPrincipal(identity);
SignInAsAuthentication(identity, properties, Options.SignInAsAuthenticationType);

// Trigger OnAuthenticated?
var eventArgs = new OnAuthenticatedEventArgs { RedirectUri = properties.RedirectUri };
Options.OnAuthenticated?.Invoke(Context, eventArgs);

// Redirect back to the original secured resource, if any
if (!string.IsNullOrWhiteSpace(properties.RedirectUri) &&
Uri.IsWellFormedUriString(properties.RedirectUri, UriKind.Absolute))
if (!string.IsNullOrWhiteSpace(eventArgs.RedirectUri) &&
Uri.IsWellFormedUriString(eventArgs.RedirectUri, UriKind.Absolute))
{
Response.Redirect(properties.RedirectUri);
Response.Redirect(eventArgs.RedirectUri);
return true;
}
}
Expand Down