逐页的基础页面上强制SSL(HTTPS) [英] Forcing SSL (https) on a page by page basis

查看:118
本文介绍了逐页的基础页面上强制SSL(HTTPS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置我的网页加载事件在我的code背后重定向到一个具有HTTPS preFIX的网址是什么?我需要它连接了过多的查询字符串的URL工作。

How can I set up my page load event in my code behind to redirect to an url that has an https prefix? I would need it to work with urls that have query strings attached too.

这是一件事,以构建径直到HTTPS页面的链接,但我不希望用户能够手动将其更改为HTTP页面。

It's one thing to construct a link that goes straight to the https page, but I don't want the user to be able to manually change it to an http page.

此外,我不希望用JavaScript这样做,因为它可能会被关闭。

Also I don't want to do it with javascript because it might be turned off.

我猜一个普通的前pression?

I'm guessing a regular expression?

推荐答案

我们纪念我们的一个特殊的属性SSL所需要的页面 ForceSslAttribute 。然后我们有一个的HttpModule 即拉低当前页面的类,并检查它的属性。

We mark our SSL Required pages with a special attribute ForceSslAttribute. Then we have a HttpModule that pulls down the current page's class and inspect it's attributes.

如果该属性是在页面上present,它需要被通过确切的网址,并从 HTTP 更改协议到 HTTPS 然后调用重定向。

If the attribute is present on the page, it takes the exact url that was passed and changes the protocol from http to https then calls a redirect.

有可能是做了一下简单的方法,但这是它是如何为我们做了。

There's probably a bit simpler way of doing it, but that's how it's done for us.

属性:

[AttributeUsage(AttributeTargets.Class, AllowMultiple=false, Inherited=true)]
public sealed class ForceSslAttribute : Attribute
{
    // Marker Attribute
}

页示例(codeBehind):

Page Example (CodeBehind):

[ForceSsl]
public partial class User_Login : Page
{
    //...
}

您可以计算出页面的类型是这样的:

You can figure out the type of the page like this:

HttpContext.Current.CurrentHandler.GetType()

所有的实施的IHttpHandler ,当您访问的页面,它会工作。

All Page's implement IHttpHandler and when you're visiting a page, it'll work.

有关此方法凉爽的部分是你可以标记任何这是一个IHttpHandler的,它会强制重定向太:)

The cool part about this method is you can mark anything that's an IHttpHandler and it'll force the redirect too :)

这篇关于逐页的基础页面上强制SSL(HTTPS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆