I have setup an Azure Active Directory (AD) to log in an MVC Web application. Once logged in, only Email ID is returned from Azure. I need to extend this to get some basic properties using Email ID as a key field; to map AD and my database which contains additional details.
My requirements are below
If you are using the OWIN middleware for OpenIdConnect, you can add your custom data as custom claims after the middleware has validated the token.
So something like:
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
// Rest of config left out for brevity
Notifications = new OpenIdConnectAuthenticationNotifications
{
SecurityTokenValidated = async (notification) =>
{
ClaimsIdentity identity = notification.AuthenticationTicket.Identity;
//Get the user data any way you want
//Add claims
identity.AddClaim(new Claim("homeTown", "Somewhere"));
}
}
});
The ClaimsIdentity
will contain the usual user information about the signed-in user. You can add any claims you want here, and they will be stored the same as the usual ones. You can then access these claims e.g. in your controllers through the User
property, or through ClaimsPrincipal.Current
anywhere else.
The main advantage here is that you only fetch the data once when the user signs in, you don't need to get them on each request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With