简体   繁体   中英

Set the login url in ASP.Net MVC Core 2.0

Due to the fact that I am not good enough in authentication related stuff and I want to port a custom authentication to the ".Net Core 2.0" I need help. There are several similar questions out there but mine is a bit different. The user can easily log in and log out to the project and just need to set the login URL for the time when the user is not logged in and should redirect to the login page.

I have already checked ( this , this or several other pages but they are mostly outdated - related to older versions - or they do not fit in my case) My Startup.cs:

// This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        var builder = services.AddMvc(options => {
            options.ModelBinderProviders.Insert(0, new Olive.Mvc.OliveBinderProvider());
        })
        .AddJsonOptions(options =>
        {
            options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
        })
        .ConfigureApplicationPartManager(manager =>
        {
            var oldMetadataReferenceFeatureProvider = manager.FeatureProviders.First(f => f is MetadataReferenceFeatureProvider);
            manager.FeatureProviders.Remove(oldMetadataReferenceFeatureProvider);
            manager.FeatureProviders.Add(new ReferencesMetadataReferenceFeatureProvider());
        }); ;

        services.AddSingleton<IUserStore<User>, UserStore>();
        services.AddSingleton<IRoleStore<string>, RoleStore>();
        services.AddIdentity<User, string>();
        services.AddAuthentication(IdentityConstants.ApplicationScheme)
            .AddCookie(opt => opt.LoginPath = "/login");

        // Adds a default in-memory implementation of IDistributedCache.
        services.AddDistributedMemoryCache();

        services.AddSession();
    }

    // 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();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseAuthentication();

        app.UseStaticFiles();

        app.UseSession();

        app.UseMvc(routes =>
        {
            //routes.MapRoute(
            //    name: "default",
            //    template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

As shown here . asp.net core 2.0 has changed it to use the ConfigureApplicationCookie method. More info about migrating Identity to Core 2.0 here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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