asp.net mvc - How to redirect HTTP to HTTPS in MVC application (IIS7.5) -
i need redirect http site https, have added below rule getting 403 error when tried using http://www.example.com, works fine when type https://www.example.com in browser.
<system.webserver> <rewrite> <rules> <rule name="http https redirect" stopprocessing="true"> <match url="(.*)" /> <conditions> <add input="{https}" pattern="off" ignorecase="true" /> </conditions> <action type="redirect" redirecttype="found" url="https://{http_host}/{r:1}" /> </rule> </rules> </rewrite> </system.webserver>
you can in code:
global.asax.cs
protected void application_beginrequest(){ if (!context.request.issecureconnection) response.redirect(context.request.url.tostring().replace("http:", "https:")); }
or add same code action filter:
public class sslfilter : actionfilterattribute { public override void onactionexecuting(actionexecutingcontext filtercontext){ if (!filtercontext.httpcontext.request.issecureconnection){ var url = filtercontext.httpcontext.request.url.tostring().replace("http:", "https:"); filtercontext.result = new redirectresult(url); } } }
Comments
Post a Comment