Click here to Skip to main content
16,016,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've integrated authO in mvc .net core 2.1 and using razor pages,unable to understand how to use authorization if i want to control razor pages access,so sign in and sign out is happening properly but id the user is not authenticated in and to keep the user on the same page , but right now when i tried to access the URL during logout i get an error when i tried access the url :https://localhost:5001/Conflicts[^]

This localhost page can’t be found
No webpage was found for the web address: https://localhost:5001/Account/Login?ReturnUrl=%2FConflicts
HTTP ERROR 404
and
the url which is generating is :https://localhost:5001/Account/Login?ReturnUrl=%2FConflicts[^]



the default page is MainPage and i want to keep the user on the same page if they are not authenticated id the try to access the URL for example Conflicts page they should remain on main page

What I have tried:

 public class Startup
   {
       public Startup(IConfiguration configuration)
       {
           Configuration = configuration;
           HostingEnvironment = HostingEnvironment;
       }

       public IConfiguration Configuration { get; }
       public IHostingEnvironment HostingEnvironment { get; }

       // This method gets called by the runtime. Use this method to add services to the container.
       public void ConfigureServices(IServiceCollection services)
       {
           services.Configure<IISOptions>(options =>
           {
               options.ForwardClientCertificate = false;
           });

           services.AddAuthentication(options =>
           {
               options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
               options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
               options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
           })
        .AddCookie()
        .AddOpenIdConnect("Auth0", options =>
        {
            // Set the authority to your Auth0 domain
            options.Authority = $"https://{Configuration["Auth0:Domain"]}";

            // Configure the Auth0 Client ID and Client Secret
            options.ClientId = Configuration["Auth0:ClientId"];
            options.ClientSecret = Configuration["Auth0:ClientSecret"];

            // Set response type to code
            options.ResponseType = "code";

            // Configure the scope
            options.Scope.Clear();
            options.Scope.Add("openid");

            // Set the callback path, so Auth0 will call back to http://localhost:5000/signin-auth0
            // Also ensure that you have added the URL as an Allowed Callback URL in your Auth0 dashboard
            options.CallbackPath = new PathString("/signin-auth0");

            // Configure the Claims Issuer to be Auth0
            options.ClaimsIssuer = "Auth0";

            // Saves tokens to the AuthenticationProperties
            options.SaveTokens = true;

            options.Events = new OpenIdConnectEvents
            {
                // handle the logout redirection
                OnRedirectToIdentityProviderForSignOut = (context) =>
             {
                 var logoutUri = $"https://{Configuration["Auth0:Domain"]}/v2/logout?client_id={Configuration["Auth0:ClientId"]}";

                 var postLogoutUri = context.Properties.RedirectUri;
                 if (!string.IsNullOrEmpty(postLogoutUri))
                 {
                     if (postLogoutUri.StartsWith("/"))
                     {
                            // transform to absolute
                            var request = context.Request;
                         postLogoutUri = request.Scheme + "://" + request.Host + request.PathBase + postLogoutUri;
                     }
                     logoutUri += $"&returnTo={ Uri.EscapeDataString(postLogoutUri)}";
                 }

                 context.Response.Redirect(logoutUri);
                 context.HandleResponse();

                 return Task.CompletedTask;
             }
            };
        });


           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;
           });

           services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
           services.AddMvc().AddRazorPagesOptions(options =>
           {
               options.Conventions.AddPageRoute("/MainPage", "");
options.Conventions.AuthorizePage("/Conflicts");
           });
       }

       // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
       public void Configure(IApplicationBuilder app, IHostingEnvironment env)
       {
           if (env.IsDevelopment())
           {
               app.UseDeveloperExceptionPage();
           }
           else
           {
               app.UseExceptionHandler("/Error");
               app.UseHsts();
           }

           app.UseHttpsRedirection();
           app.UseStaticFiles();
           app.UseCookiePolicy();
           app.UseCorsMiddleware();
           app.UseAuthentication();
           app.UseCors("CorsPolicy");
           app.UseMvc();
           //app.UseMvc(rb =>
           //{
           //    rb.MapRoute(
           //        name: "default",
           //        template: "{controller}/{action}/{id?}",
           //        defaults: new { controller = "Home", action = "Index" });
           //});
       }
   }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900