如何在c#中使用https?

tho*_*lau 5 c# asp.net-mvc https asp.net-mvc-4

我正在开发一个Web应用程序.为了用户信息的安全,我需要一个https连接.我现在正在开发这个地方.我已按照以下教程:http://weblogs.asp.net/dwahlin/archive/2009/08/25/requiring-ssl-for-asp-net-mvc-controllers.aspx

当我构建我的项目页面加载但网址是:http:// ...

在我的代码我已经放置:

    [RequiresSSL]
    public ActionResult Index()
    {
        //var model = Adapter.EuserRepository.GetAll();

        return View(db.Eusers.ToList());
    }
Run Code Online (Sandbox Code Playgroud)

网站代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace extranet.Helpers
{
    public class RequiresSSL: ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpRequestBase req = filterContext.HttpContext.Request;
            HttpResponseBase res = filterContext.HttpContext.Response;

            //Check if we're secure or not and if we're on the local box
            if (!req.IsSecureConnection && !req.IsLocal)
            {
                var builder = new UriBuilder(req.Url)
                {
                    Scheme = Uri.UriSchemeHttps,
                    Port = 443
                };
                res.Redirect(builder.Uri.ToString());
            }
            base.OnActionExecuting(filterContext);
        }


    }
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么网址不是基于https的?这是因为我在当地工作吗?

提前致谢

Joe*_*ari 3

您的过滤器使用以下语句检查请求是否是本地请求:&& !req.IsLocal。如果是,则不会重定向。如果删除该语句,则无论您是否在本地,都将需要通过 HTTPS 访问该操作。