2017-09-14 29 views
0

由於我在身份驗證相關內容方面不夠好,而且我想將自定義身份驗證移植到「.Net Core 2.0」,所以我需要幫助。有幾個類似的問題,但我的情況有點不同。用戶可以輕鬆登錄和註銷項目,只需設置用戶未登錄時的登錄URL並重定向到登錄頁面即可。在ASP.Net MVC Core 2.0中設置登錄網址

我已經檢查(thisthis或其他幾個頁面,但他們大多是過時的 - 與舊版本 - 或者他們不適合在我的情況) 我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?}"); 
     }); 
    } 
+0

您是否將Authorize屬性添加到要保護的控制器和操作方法? – ssimeonov

+0

是的,我做到了。問題在於用戶被重定向到錯誤的URL(「/ Account/Login」而不是「/ Login」)。 –

+1

https://stackoverflow.com/a/45922216/9604有幫助嗎? – mxmissile

回答

2

由於shown hereasp.net core 2.0已將其更改爲使用ConfigureApplicationCookie方法。有關將身份遷移到Core 2.0 here的更多信息。