How to not make login or register mandatory in asp.net core 2.1?

Viewed 313

I added to my project that only logged in users where able to view my website. But now I want to make it so the user doesn't need to be logged in. I already removed thing that I added to my Startup class, and now it gives me an error saying "localhost:5000/Account/Login not found". Here is my startup class:

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

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        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.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));

        services.AddDefaultIdentity<IdentityUser>()
        .AddEntityFrameworkStores<ApplicationDbContext>();
        //.AddRoles<IdentityRole>()

        services.AddControllersWithViews().AddNewtonsoftJson(options =>
            options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        );
        services.AddSignalR();

        //services.AddControllersWithViews();
        //services.AddRazorPages();


        //services.AddControllers(config =>
        //{
        //    // using Microsoft.AspNetCore.Mvc.Authorization;
        //    // using Microsoft.AspNetCore.Authorization;
        //    var policy = new AuthorizationPolicyBuilder()
        //                     .RequireAuthenticatedUser()
        //                     .Build();
        //    config.Filters.Add(new AuthorizeFilter(policy));
        //});



        //premite o acesso ao controller IgnicoesAPIController através dos outros controllers
        services.AddTransient<IgnicoesAPIController, IgnicoesAPIController>();
        services.AddTransient<OcorrenciasAPIController, OcorrenciasAPIController>();
        services.AddSingleton<IConfiguration>(Configuration);
    }

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

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseAuthentication();

        //SIGNAL R

        //String caminho = configuration["AppSettings:Servidor"] + "/myHub";
        String caminho = Configuration["AppSettings:Servidor"] + "/myHub";

        //endpoints.MapHub<MyHub>(caminho);
        //app.UseSignalR(route =>
        //{
        //    route.MapHub<MyHub>(caminho);

        //});

        app.UseRouting();
       // app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<MyHub>("/myHub");
            endpoints.MapControllerRoute(
                name: "default",
               pattern: "{controller=Home}/{action=Index}/{id?}");
            //endpoints.MapRazorPages();
        });

       

    }
}

Am I missing something?

1 Answers

I added to my project that only logged in users where able to view my website. But now I want to make it so the user doesn't need to be logged in.

It seems that you do configuration globally to require all users to be authenticated via authorize filter, which would redirect to non-authenticated users to login page.

If you'd like to allow non-authenticated users to access your MVC website, you can try to comment out the configuration of requiring all users to be authenticated. Or apply the AllowAnonymous attribute at the controller level to bypasses all authorization statements.

[AllowAnonymous]
public class HomeController : Controller
{
    //...
Related