注册和登录页面使用https asp.net 4.0

时间:2012-06-19 14:19:35

标签: c# asp.net https

我有Godaddy的虚拟主机,我带了一个ssl证书和我的域名。是否有一个简单的方法让login.aspx页面和register.aspx页面转到https?我不想明确说明重定向(“https://domain/login.aspx)。感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

最简单的方法是使用以下代码修改这些页面(如果未在本地运行,则重定向到https,而不是安全连接):

if (!Request.IsLocal && !Request.IsSecureConnection)
{
    string redirectUrl = Request.Url.ToString().Replace("http:", "https:");
    Response.Redirect(redirectUrl);
}

答案 1 :(得分:0)

通常最简单的解决方案是最好的,但如果你想要坚果......

您可以编写HTTP模块以确保将特定页面的列表重定向到SSL。

public class EnsureSslModule : IHttpModule
{
    private static readonly string[] _pagesToEnsure = new[] { "login.aspx", "register.aspx" };

    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += OnBeginRequest;
    }

    public void OnBeginRequest(object sender, EventArgs e)
    {
        var application = (HttpApplication)sender;
        var context = application.Context;

        var url = context.Request.RawUrl;

        if (!context.Request.IsSecureConnection 
                && _pagesToEnsure.Any(page => url.IndexOf(page, StringComparison.InvariantCultureIgnoreCase) > -1))
        {
            var builder = new UriBuilder(url);

            builder.Scheme = Uri.UriSchemeHttps;

            context.Response.Redirect(builder.Uri
                .GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Port,
                               UriFormat.UriEscaped), true);
        }
    }
}
相关问题