简体   繁体   English

如何在asp.net core 2.1中不强制登录或注册?

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

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".但现在我想让用户不需要登录。我已经删除了我添加到 Startup 类的东西,现在它给我一个错误说“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?我错过了什么吗?

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.如果您希望允许未经身份验证的用户访问您的 MVC 网站,您可以尝试将要求所有用户都进行身份验证的配置注释掉。 Or apply the AllowAnonymous attribute at the controller level to bypasses all authorization statements.或者在控制器级别应用AllowAnonymous属性来绕过所有授权语句。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM