Jac*_*raw 2 c# asp.net ssl redirect
我一直在努力为ASP.Net应用程序重定向到SSL页面代码一段时间.我还没有找到一个好的解决方案.问题是,如果我在localhost上,我希望禁用代码,例如我在调试或编码时,但我无法使其工作.另一个问题是它必须重定向到另一个URL,因此http://www.xx.com应该重定向到https://Secure.xxx.com.有任何想法吗?
如果要在页面级别设置ssl属性,则应创建自定义属性.
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class HttpsAttribute : Attribute{}
[HttpsAttribute]
public partial class PageSecured: YourCustomBasePage {}
Run Code Online (Sandbox Code Playgroud)
现在基页YourCustomBasePage可以检查是否attribute已设置:
protected override void OnInit(EventArgs e){
if(!Request.IsSecureConnection){
HttpsAttribute secureAttribute = (HttpsAttribute)Attribute.GetCustomAttribute(GetType(), typeof(HttpsAttribute));
if(secureAttribute != null){
//Build your https://Secure.xxx.com url
UriBuilder httpsUrl = new UriBuilder(Request.Url){Scheme = Uri.UriSchemeHttps, Port = 443};
Response.Redirect(httpsUrl.Uri.ToString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
要将本地计算机排除在重定向之外HTTPS,您可以在您的计算机中使用配置值web.config.
private bool HTTPSEnabled{
get{ return ConfigurationManager.AppSettings["HTTPSEnabled"] == null || Boolean.Parse(ConfigurationManager.AppSettings["HTTPSEnabled"]); }
}
Run Code Online (Sandbox Code Playgroud)
并将检查添加到第一个条件
if(!Request.IsSecureConnection && HTTPSEnabled)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
874 次 |
| 最近记录: |