Best way to restrict access by IP address?

One way is using a HttpModule. From the link (in case it ever goes away): /// <summary> /// HTTP module to restrict access by IP address /// </summary> public class SecurityHttpModule : IHttpModule { public SecurityHttpModule() { } public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(Application_BeginRequest); } private void Application_BeginRequest(object source, EventArgs e) { … Read more

difference between http.context.user and thread.currentprincipal and when to use them?

The first thing that the HttpApplication object does when it acquires a thread is to set the thread’s principal to the HttpContext’s principal. This syncs up the principals. If, however, you go and set the Thread’s principal later on, the HttpApplication internally still has a different principal set for the user context. This is why … Read more

How to get hold of Content that is already read

You could read from the underlying request: using (var stream = new MemoryStream()) { var context = (HttpContextBase)Request.Properties[“MS_HttpContext”]; context.Request.InputStream.Seek(0, SeekOrigin.Begin); context.Request.InputStream.CopyTo(stream); string requestBody = Encoding.UTF8.GetString(stream.ToArray()); }

Change Single URL query string value

You can’t modify the QueryString directly as it is readonly. You will need to get the values, modify them, then put them back together. Try this: var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString()); nameValues.Set(“page”, “2”); string url = Request.Url.AbsolutePath; string updatedQueryString = “?” + nameValues.ToString(); Response.Redirect(url + updatedQueryString); The ParseQueryString method returns a NameValueCollection (actually it really returns … Read more

How do I maintain ModelState errors when using RedirectToAction?

The PRG pattern is ok, but I did this: Base controller: protected override void OnActionExecuted(ActionExecutedContext filterContext) { if (TempData[“ModelState”] != null && !ModelState.Equals(TempData[“ModelState”])) ModelState.Merge((ModelStateDictionary)TempData[“ModelState”]); base.OnActionExecuted(filterContext); } Action (I’m using xVal): try { user.Login(); AuthenticationManager.SignIn(user); } catch (RulesException rex) { // on bad login rex.AddModelStateErrors(ModelState, “user”); TempData[“ModelState”] = ModelState; return Redirect(Request.UrlReferrer.ToString()); } The action throws an … Read more

HttpContext.Current.User.Identity.Name is always string.Empty

FormsAuthentication.SetAuthCookie(tbUsername.Text, true); bool x = User.Identity.IsAuthenticated; //true string y = User.Identity.Name; //”” The problem you have is at this point you’re only setting the authentication cookie, the IPrincipal that gets created inside the forms authentication module will not happen until there is a new request – so at that point the HttpContext.User is in a … Read more

Default redirect for Error 404

This is how you configure a custom 404 error page for both ASP.NET and non-ASP.NET requests: <configuration> <system.web> <compilation targetFramework=”4.0″ /> <customErrors mode=”On” redirectMode=”ResponseRewrite”> <error statusCode=”404″ redirect=”http404.aspx” /> </customErrors> </system.web> <system.webServer> <httpErrors errorMode=”Custom”> <remove statusCode=”404″/> <error statusCode=”404″ path=”/http404.aspx” responseMode=”ExecuteURL”/> </httpErrors> </system.webServer> </configuration> As others already pointed out, you should not use an HTTP redirection to … Read more

IIS AAR – URL Rewrite for reverse proxy – how to send HTTP_HOST

This post has the answer – Modifying headers with IIS7 Application Request Routing Need to enable preserveHostHeader – can’t see how you do that in the UI but this works Run this from command line to update Machine/webroot/apphost config %windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/proxy -preserveHostHeader:true /commit:apphost