2015-11-12 41 views
8

HTTPS我如何試圖做到這一點:在啓動如何啓用mvc6

1設置過濾器:

 public IServiceProvider ConfigureServices(IServiceCollection services) 
     { 
     //... 
     services.AddMvc(); 
     services.Configure<MvcOptions>(options => 
     { 
      options.Filters.Add(new RequireHttpsAttribute()); 
     }); 

2 - 集[RequireHttps]在cotroler

[RequireHttps] 
public class HomeController : BaseController 
{ 
    public ViewResult Index() 
    { 
     return View(); 
    } 
} 

3 - add in project.json

"kestrel": "Microsoft.AspNet.Hosting --server=Microsoft.AspNet.Server.Kestrel --server.urls=https://localhost:1234" 

而且還沒有工作G。 我做錯了什麼?

+0

此問題適用於MVC6。在這種情況下,您添加了儘可能重複的答案沒有幫助。 – Raskolnikov

回答

5

編輯:這是一個新功能,尚未在beta8尚未。我在Github的beta8標籤中試圖找到這個功能後就注意到了。看起來你現在唯一的解決方案是支持IIS(支持HTTPS)或者支持NGINX,同時爲你添加該模塊。

確保在您的Startup.cs/Configure方法中啓用SSL。

它可以這樣判定:

var certPath = "c:\\mycert.pfx"; 
app.UseKestrelHttps(new X509Certificate2(certPath, "certificatePassword")); 

行動過濾器將只作用於實際的URL。你確實需要監聽一個帶有證書的端口才能訪問HTTP。

希望這會有所幫助。

Source to sample Startup.cs