0

I have a requirement where the public users will click on login then it will go to SSO and after that it will redirect back to the website with details. I am using the OpenID connector for public user SSO for this I have created Startup file.

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
           CorePipeline.Run("initializeOwinMiddlewareCustom", new InitializeOwinMiddlewareArgs(app));
        }
    }

and below is InitializeOwinMiddlewareCustom class.

    public void Process(InitializeOwinMiddlewareArgs args)
    {
         args.App.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
         args.App.UseCookieAuthentication(new CookieAuthenticationOptions());
         args.App.UseOpenIdConnectAuthentication(
               new OpenIdConnectAuthenticationOptions
               {
                   Authority = authority,
                   ClientId = clientId,
                   ClientSecret = clientSecret,
                   ResponseType = OpenIdConnectResponseType.IdTokenToken,
                   RedirectUri = redirectUri,
                   PostLogoutRedirectUri = postLogoutRedirectUri,
                   Scope = OpenIdConnectScope.OpenIdProfile + " " + OpenIdConnectScope.OfflineAccess,
                   SaveTokens = true,
                   TokenValidationParameters = new TokenValidationParameters()
                   {
                       ValidateIssuer = false 
                   },
                   Notifications = new OpenIdConnectAuthenticationNotifications
                   {
                       AuthenticationFailed = OnAuthenticationFailed,
                       SecurityTokenValidated = notification =>
                       {
                           notification.AuthenticationTicket.Identity.AddClaim(new Claim("surname", notification.AuthenticationTicket.Identity.Claims.FirstOrDefault(x => x.Type == "surname").Value));
                           notification.AuthenticationTicket.Identity.AddClaim(new Claim("id_token", notification.ProtocolMessage.IdToken));
                           notification.AuthenticationTicket.Identity.AddClaim(new Claim("phone_number", notification.AuthenticationTicket.Identity.Claims.FirstOrDefault(x => x.Type == "phone_number").Value));
                           return Task.FromResult(0);
                       }
                   }
               }
           );
        }

Login Controller:

  if (!Request.IsAuthenticated)
  {
       HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" },
       OpenIdConnectAuthenticationDefaults.AuthenticationType);
  }

I am getting all claim in OpenIdConnectAuthenticationNotificationsbut in controllerRequest.IsAuthenticated is coming false.

if(Request.IsAuthenticated)
{
    var claims= ClaimsPrincipal.Current.Identities.First().Claims.ToList();
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.