安全Web应用程序

时间:2010-07-09 14:27:48

标签: asp.net

我正在使用在线支付构建应用程序(asp.net),因此我想使这些支付页面启用SSL。我怎样才能做到这一点.. 救救我..

由于

1 个答案:

答案 0 :(得分:1)

创建类似于

的类
public class SecurePage : Page
{
    protected override void OnInit( EventArgs e )
    {
        //retrieve appsettings value. set to false for localhost or dev environment
        var sslRequired = bool.Parse(WebConfigurationManager.AppSettings["Security-SSL-Required"] ?? "true");
        var currentUrl = System.Web.HttpContext.Current.Request.Url;

        if (!currentUrl.Scheme.Equals(Uri.UriSchemeHttps, StringComparison.CurrentCultureIgnoreCase) && sslRequired)
        {
            //build the secure uri 
            var secureUrlBuilder = new UriBuilder(currentUrl);
            secureUrlBuilder.Scheme = Uri.UriSchemeHttps;
            //use the default port.  
            secureUrlBuilder.Port = -1;
            //redirect and end the response. 
            System.Web.HttpContext.Current.Response.Redirect(secureUrlBuilder.Uri.ToString());
        }

        base.OnInit(e);
    }
}

然后,对于需要从此基类继承SSL的页面:

public class Login : SecurePage
{
    protected void Page_Load
        (
        object sender,
        EventArgs e )
    {
    }
}