![](/img/trans.png)
[英]What is the best way to return values from another page in code behind in asp.net?
[英]What is the best way to code a redirect to SSL page in ASP.Net
對於ASP.Net應用程序,我一直在努力重定向到SSL頁面代碼。 我仍然沒有找到一個好的解決方案。 問題是,如果我在本地主機上,例如在調試或編碼時,我想禁用代碼,但是我無法使它正常工作。 另一個問題是它必須重定向到另一個URL,因此http://www.xx.com應該重定向到https://Secure.xxx.com 。 有任何想法嗎?
如果要在頁面級別設置ssl屬性,則應創建一個custom屬性 。
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class HttpsAttribute : Attribute{}
[HttpsAttribute]
public partial class PageSecured: YourCustomBasePage {}
現在,基礎頁面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());
}
}
}
要排除本地計算機重定向到HTTPS
,可以在web.config
使用config值。
private bool HTTPSEnabled{
get{ return ConfigurationManager.AppSettings["HTTPSEnabled"] == null || Boolean.Parse(ConfigurationManager.AppSettings["HTTPSEnabled"]); }
}
然后將支票添加到第一個條件
if(!Request.IsSecureConnection && HTTPSEnabled)
我在生產中使用了這個庫。 我更喜歡它設置屬性,因為它是配置驅動的,並且可以配置到很好的水平。 話雖這么說,我不確定它是否可以重定向到子域。 我什至不知道您為什么要那樣做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.