HTTP module re-writing the URL with some encryption - asp.net

I am writing one class with the help of HTTPModule to check userIdentity in session before he access any page.If the variable in the session is null or empty i am redirecting the user in session expired page.
Code in Class:
public class SessionUserValidation : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication application)
{
application.PreRequestHandlerExecute += new
EventHandler(application_PreRequestHandlerExecute);
}
private void application_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
IHttpHandler handler = application.Context.Handler;
Page reqPage = handler as Page;
if (reqPage != null)
{
reqPage.PreInit += new EventHandler(CustomModule_Init);
}
}
private void CustomModule_Init(object sender, EventArgs e)
{
Page Page = sender as Page;
if (!Page.Request.Url.ToString().Contains("mySessionExpired.aspx") &&
!Page.Request.Url.ToString().Contains("myLogin.aspx"))
{
if (HttpContext.Current.Session["encryptedUserId"] == null)
{
HttpContext.Current.Response.Redirect("../Modulenames/mySessionExpired.aspx", false);
}
}
}
}
everything is working fine , only issue is that its adding some kind of encryption in URL for which my Breadcrumbs are not working in the page. The url transforms like :
://thewebsite/Project/(S(jnd4o5ljdgs0vq1zd4niby4a))/Pages/mySessionExpired.aspx
no idea why this fragment of text has been added ... please help
--Attu

Related

display next page asynchronously and then run its page load event

I have an airline site in which i am trying to display the fare from different API/Web service. But Firstly i want to display the search page--> Display processing --> binding data in the page from theses API/web service as they received.
but i am not able to display search page before the result processing.
What i have tried (code)-
public partial class asyncgridview : System.Web.UI.Page,ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsCallback)
{
ltCallback.Text = ClientScript.GetCallbackEventReference(this, "'bindgrid'", "EndGetData", "'asyncgrid'", false);
}
}
private string _Callback;
public string GetCallbackResult()
{
return _Callback;
}
public void RaiseCallbackEvent(string eventArgument)
{
DataTable table = fetchData();
gvAsync.DataSource = table;
gvAsync.DataBind();
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
gvAsync.RenderControl(new HtmlTextWriter(sw));
_Callback = sw.ToString();
}
}
}
regards
Avishek

How to call a page method in the dynamically addedded usercontrol?

I am adding a user control dynamically on a page, the user control has a save button that takes data from both user control and page to save in the DB, in the same save method i want to access a method wrriten in the page, so i that mehod had code to rebind the grid kept in the page.
So how can i call a page method in the dynamically added user control?
I was going to suggest creating a base class for your pages, but found an even better way to accomplish this task:
http://www.codeproject.com/Articles/115008/Calling-Method-in-Parent-Page-from-User-Control
Control code:
public partial class CustomUserCtrl : System.Web.UI.UserControl
{
private System.Delegate _delWithParam;
public Delegate PageMethodWithParamRef
{
set { _delWithParam = value; }
}
private System.Delegate _delNoParam;
public Delegate PageMethodWithNoParamRef
{
set { _delNoParam = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnMethodWithParam_Click(object sender, System.EventArgs e)
{
//Parameter to a method is being made ready
object[] obj = new object[1];
obj[0] = "Parameter Value" as object;
_delWithParam.DynamicInvoke(obj);
}
protected void BtnMethowWithoutParam_Click(object sender, System.EventArgs e)
{
//Invoke a method with no parameter
_delNoParam.DynamicInvoke();
}
}
Page code:
public partial class _Default : System.Web.UI.Page
{
delegate void DelMethodWithParam(string strParam);
delegate void DelMethodWithoutParam();
protected void Page_Load(object sender, EventArgs e)
{
DelMethodWithParam delParam = new DelMethodWithParam(MethodWithParam);
//Set method reference to a user control delegate
this.UserCtrl.PageMethodWithParamRef = delParam;
DelMethodWithoutParam delNoParam = new DelMethodWithoutParam(MethodWithNoParam);
//Set method reference to a user control delegate
this.UserCtrl.PageMethodWithNoParamRef = delNoParam;
}
private void MethodWithParam(string strParam)
{
Response.Write(“<br/>It has parameter: ” + strParam);
}
private void MethodWithNoParam()
{
Response.Write(“<br/>It has no parameter.”);
}
}

Secure webApplication

I am building an application(asp.net) with online payment so i want to make these payment page SSL enable. How can i do that..
Help me..
Thanks
Create class similar to
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);
}
}
Then for pages that require SSL inherit from this base class:
public class Login : SecurePage
{
protected void Page_Load
(
object sender,
EventArgs e )
{
}
}

load different css for site localization

I need to load different css file depending on the language that the user selects. I need to do this only in my master page.
If you are using the built-in themes and globalization support you could use a httpModule: (untested)
public class PageModule : IHttpModule
{
public void Dispose()
{
}
public void Init(System.Web.HttpApplication context)
{
context.PreRequestHandlerExecute += Application_PreRequestHandlerExecute;
}
public void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
//Adds a handler that executes on every page request
HttpApplication application = default(HttpApplication);
application = (HttpApplication)sender;
Page page = application.Context.CurrentHandler as Page;
if ((page != null))
page.PreInit += Page_PreInit;
}
public void Page_PreInit(object sender, EventArgs e)
{
//If current context has no session then abort
if (HttpContext.Current.Session == null)
return;
//Get current page context
Page page = (Page)sender;
switch (page.Culture) {
case "en-US":
page.Theme = "en-USTheme";
break;
case "fr-FR":
page.Theme = "fr-FRTheme";
break;
default:
page.Theme = "DefaultTheme";
break;
}
}
}
you could write the selected language in a cookie. Then in you master page inspect the value saved in the cookie and assign the correct stylesheet.

How to redirect with HttpContext

My questions are:
So I need to create a custom class / HttpHandler and throw this code in it? Or can I place this somewhere else like in the global.asax?
How do I check for the Host (so check for www.mydomain.com) incoming so I know when to redirect?
Code:
if ("comes from certain domain")
{
context.Response.Status = "301 Moved Permanently";
context.Response.AddHeader("Location", "http://www.testdomain.com/Some.aspx");
}
Paste this into a new .cs file in your App_Code folder:
using System;
using System.Web;
public class TestModule : IHttpModule
{
public void Init(HttpApplication context) {
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e) {
HttpApplication app = (HttpApplication)sender;
if (app.Request.Url.Host == "example.com") {
app.Response.Status = "301 Moved Permanently";
app.Response.AddHeader("Location", "http://www.testdomain.com/Some.aspx");
}
}
public void Dispose() {
}
}
Then add this to your web.config in system.web:
<httpModules>
<add type="TestModule" name="TestModule" />
</httpModules>
You should be able to place it in the Global.asax, in the Application_BeginRequest event.
protected void Application_BeginRequest(Object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
string host = context.Request.Url.Host;
if (host == "www.mydomain.com")
{
context.Response.Status = "301 Moved Permanently";
context.Response.AddHeader("Location",
"http://www.testdomain.com/Some.aspx");
}
}
I avoid putting anything unnecessary in global.asax as it tends to get cluttered. Instead, create an HttpModule, add an event handler
public void Init(System.Web.HttpApplication app)
{
app.BeginRequest += new System.EventHandler(Rewrite_BeginRequest);
}
and, in the beginRequest method,
public void Rewrite_BeginRequest(object sender, System.EventArgs args)
{
HttpApplication app = (HttpApplication)sender;
/// all request properties now available through app.Context.Request object
}

Resources