Set the login url in ASP.Net MVC Core 2.0

11,069

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.

Share:
11,069
Hamid Mayeli
Author by

Hamid Mayeli

Please do read my bio on LinkedIn, if you are interested to know me. https://www.linkedin.com/in/hamidmayeli/

Updated on June 05, 2022

Comments

  • Hamid Mayeli
    Hamid Mayeli almost 2 years

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