Asp.net web api 如何设置ASP.NET Web Api应用程序以通过HTTPS运行

Asp.net web api 如何设置ASP.NET Web Api应用程序以通过HTTPS运行,asp.net-web-api,Asp.net Web Api,我已经使用MicrosoftWebAPI编写了一个小的web应用程序,但我对这个平台还不熟悉。我如何设置它,使其仅在https上工作,并且如果用户试图通过http访问它,他/她将被重定向?这就是您要查找的内容吗 public override void OnActionExecuting(HttpActionContext actionContext) { //First, check the string for presence of HTTPS if (!String.Eq

我已经使用MicrosoftWebAPI编写了一个小的web应用程序,但我对这个平台还不熟悉。我如何设置它,使其仅在https上工作,并且如果用户试图通过http访问它,他/她将被重定向?

这就是您要查找的内容吗

public override void OnActionExecuting(HttpActionContext actionContext)
{
    //First, check the string for presence of HTTPS
    if (!String.Equals(actionContext.Request.RequestUri.Scheme, "https", StringComparison.OrdinalIgnoreCase))
    {
       //Tell the user they can't have your API - Error 400.
       actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest)
       {
       //Tell them so.
       Content = new StringContent("HTTPS Required")
    };
    return;
}

编辑:这不是原创的,但我以前有效地使用过。希望有帮助。

这是由web服务器软件而不是应用程序控制的最佳方式。