【问题标题】:How to use https in c#?如何在 C# 中使用 https?
【发布时间】:2013-04-15 08:40:45
【问题描述】:

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

当我构建我的项目时,页面会加载,但 url 是:http://...

在我放置的代码中:

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

        return View(db.Eusers.ToList());
    }

来自网站的代码:

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);
        }


    }
}

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

提前致谢

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 https


    【解决方案1】:

    当我删除那段代码时,我得到一个 401 - 无法生成 与服务器 localhost 的连接,但链接现在是 https

    删除这部分代码只是解决问题的一部分。回到你最初的“如何使用 https” 的问题,那么你需要 enable it using this guide

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 哇...我怎么看不到耻辱,当我删除那段代码时,我得到一个 401 - 无法与服务器 localhost 建立连接,但链接现在是 https...
      • 您是否安装了(测试)证书?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 2014-05-24
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多