4

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?}");
        });
    }
3
  • Did you add Authorize attribute to the Controllers and Action methods that you want to protect? Commented Sep 14, 2017 at 12:01
  • Yes I did. The problem is that the user is redirected to wrong URL ("/Account/Login" rather than "/Login"). Commented Sep 14, 2017 at 12:31
  • 1
    stackoverflow.com/a/45922216/9604 help? Commented Sep 14, 2017 at 16:33

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.