You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
recently we have migrated our project from asp.net to asp.net core, the project was working fine in asp.net, We have followed the migration document of Microsoft for migrating "asp.net to asp.net core" and modified accordingly, unfortunately, OnRedirectToIdentityProvider method in startup class is not working on challenge call (from the controller). It would be great if someone helps me to figure out where my code is wrong. I have been looping on the same issue for a while. thanks in advance.
StartUp.cs
publicclassStartup{publicStartup(IConfigurationconfiguration){Configuration=configuration;}publicIConfigurationConfiguration{get;}// This method gets called by the runtime. Use this method to add services to the container.publicvoidConfigureServices(IServiceCollectionservices){services.Configure<CookiePolicyOptions>(options =>{// This lambda determines whether user consent for non-essential cookies is needed for a given request.options.CheckConsentNeeded= context =>true;options.MinimumSameSitePolicy=SameSiteMode.None;});//Add a strongly-typed options class to DIservices.Configure<AuthOptions>(Configuration.GetSection("Authentication"));services.AddAuthentication(opt =>{opt.DefaultScheme=CookieAuthenticationDefaults.AuthenticationScheme;opt.DefaultChallengeScheme=OpenIdConnectDefaults.AuthenticationScheme;}).AddCookie("MiddleWareCookie").AddOpenIdConnect(options =>Configuration.Bind("Authentication",options));services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>{options.Authority=Configuration["Authentication:Authority"];options.ClientId=Configuration["Authentication:ClientId"];options.ClientSecret=Configuration["Authentication:ClientSecret"];options.TokenValidationParameters=newTokenValidationParameters{// Instead of using the default validation (validating against a single issuer value, as we do in// line of business apps), we inject our own multitenant validation logicValidateIssuer=false,// If the app is meant to be accessed by entire organizations, add your issuer validation logic here.//IssuerValidator = (issuer, securityToken, validationParameters) => {// if (myIssuerValidationLogic(issuer)) return issuer;//}};options.Events=newOpenIdConnectEvents{OnRedirectToIdentityProvider=(context)=>{objectobj=null;varrequest=context.Request;if(context.HttpContext.Items.TryGetValue("Authority",outobj)){stringauthority=objasstring;if(authority!=null){context.ProtocolMessage.IssuerAddress=authority;}}//string appBaseUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.PathBase;stringappBaseUrl=@"https://localhost:44359/";//UriHelper.BuildAbsolute(request.Scheme, request.Host, request.PathBase);Debug.WriteLine($"appBaseUrl: {appBaseUrl}");context.ProtocolMessage.PostLogoutRedirectUri=appBaseUrl;context.ProtocolMessage.Prompt="select_account";context.ProtocolMessage.Resource=Configuration["Authentication:AzureResourceManagerIdentifier"];returnTask.FromResult(0);},OnAuthorizationCodeReceived=async(context)=>{varrequest=context.HttpContext.Request;varcurrentUri=UriHelper.BuildAbsolute(request.Scheme,request.Host,request.PathBase,request.Path);varcredential=newClientCredential(context.Options.ClientId,context.Options.ClientSecret);stringtenantId=context.Principal.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;//CommentDebug.WriteLine($"tenantID: {tenantId}");// RevisitstringsignedInUserUniqueName=context.Principal.FindFirst(ClaimTypes.Name).Value.Split('#')[context.Principal.FindFirst(ClaimTypes.Name).Value.Split('#').Length-1];//CommentDebug.WriteLine($"tenantID: {signedInUserUniqueName}");vartokenCache=newADALTokenCache(signedInUserUniqueName);tokenCache.Clear();// revisitAuthenticationContextauthContext=newAuthenticationContext(string.Format("https://login.microsoftonline.com/{0}",tenantId),tokenCache);// var items = authContext.TokenCache.ReadItems().ToList();// revisitAuthenticationResultresult=awaitauthContext.AcquireTokenByAuthorizationCodeAsync(context.ProtocolMessage.Code,newUri(currentUri),credential);//Tell the OIDC middleware we got the tokens, it doesn't need to do anythingcontext.HandleCodeRedemption(result.AccessToken,result.IdToken);},OnTokenValidated=(context)=>{stringissuer=context.Principal.FindFirst("iss").Value;if(issuer!=null){if(!issuer.StartsWith("https://sts.windows.net/"))thrownewSecurityTokenValidationException();}returnTask.FromResult(0);},OnTicketReceived= context =>{// If your authentication logic is based on users then add your logic herereturnTask.CompletedTask;},OnAuthenticationFailed= context =>{context.Response.Redirect("/Error");context.HandleResponse();// Suppress the exceptionreturnTask.CompletedTask;},// If your application needs to do authenticate single users, add your user validation below.//OnTokenValidated = context =>//{// return myUserValidationLogic(context.Ticket.Principal);//}};});services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.publicvoidConfigure(IApplicationBuilderapp,IHostingEnvironmentenv){if(env.IsDevelopment()){app.UseDeveloperExceptionPage();}else{app.UseExceptionHandler("/Home/Error");// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.app.UseHsts();}app.UseHttpsRedirection();app.UseStaticFiles();app.UseCookiePolicy();app.UseAuthentication();app.UseMvc(routes =>{routes.MapRoute(name:"default",template:"{controller=Home}/{action=Index}/{id?}");});}}
Uh oh!
There was an error while loading. Please reload this page.
recently we have migrated our project from asp.net to asp.net core, the project was working fine in asp.net, We have followed the migration document of Microsoft for migrating "asp.net to asp.net core" and modified accordingly, unfortunately, OnRedirectToIdentityProvider method in startup class is not working on challenge call (from the controller). It would be great if someone helps me to figure out where my code is wrong. I have been looping on the same issue for a while. thanks in advance.
StartUp.cs
Subscription Contoller Method
The text was updated successfully, but these errors were encountered: