I am using Owin, OpenId authentication for my asp.net application to validate the users with Azure login. but once i login is done from azure and redirects, the AuthorizationCodeReceived goes into a infinite loop. Below is the code that i have used.
I have tried various suggestions from different posts as below but that has not helped me.
https://github.com/IdentityServer/IdentityServer3/issues/3239
infinite loop going back to authentication page when using OAuth in MVC5
- Second sign-in causes infinite redirect loop after the first successful login MVC .NET 5 OWIN ADAL OpenIDConnect
setting the CallbackPath
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseKentorOwinCookieSaver(); //did not work app.UseCookieAuthentication(new CookieAuthenticationOptions() { //CookieHttpOnly = false, //CookieSecure = CookieSecureOption.SameAsRequest, //Did not work //CookieManager = new SystemWebCookieManager() //did not work AuthenticationType = "Cookies" } ); app.UseOpenIdConnectAuthentication( new OpenIdConnectAuthenticationOptions { ClientId = clientId, Authority = authority, PostLogoutRedirectUri = postLogoutRedirectUri, RedirectUri = postLogoutRedirectUri, CallbackPath = new PathString("/my_Azure/Start.aspx"), Notifications = new OpenIdConnectAuthenticationNotifications() { // // If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away. // AuthorizationCodeReceived = (context) => { var code = context.Code; ClientCredential credential = new ClientCredential(clientId, appKey); string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value; Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority, new ADALTokenCache(signedInUserID)); AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode( code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId); return Task.FromResult(0); } } } ); // This makes any middleware defined above this line run before the Authorization rule is applied in web.config app.UseStageMarker(PipelineStage.Authenticate);