OK, here's the thing... You fetch the user's IP address and based on his/her country you redirect the user to a specific webpage. Now, how do you change the master page dynamically? This is how I am redirecting the user :-
Geolocation error with IP address 127.0.0.1
It's not like the user clicks some button or something and you then change the master page. I want it changed when the user is redirected, so how exactly do I go about it?
public partial class testClass: System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (redirected to site1)
{
Use this master page1
}
else
if (redirected to site2)
{
Use this master page2
}
}
}
So how do I check what SITE the user has been redirected to? Also HOW to apply the specific master page now that the user has been redirected?
I just need an idea how to go about it.
[EDIT] please check the code block below. How do I fetch the URL that the user has been redirected to? I actually need just the "iso3166TwoLetterCode" variable's value (see the link to my earlier question, please) and based on that will be changing the master page. I can't figure out how to fetch that value or even use that class (that's got this variable) in my testClass.
protected void Page_PreInit(object sender, EventArgs e)
{
if (user Has been redirected to www.site.in )
{
this.MasterPageFile = "master1.master";
}
if (user Has been redirected to www.site.fr )
{
this.MasterPageFile = "master2.master";
}
}
To find out the country two letter code, do what this sample code from http://ipaddressextensions.codeplex.com/ does:
using System.Net;
using WorldDomination.Net;
string userHostIpAddress = "203.1.2.3";
IPAddress ipAddress;
if (IPAddress.TryParse(userHostIpAddress, out ipAddress))
{
string country = ipAddress.Country(); // return value: UNITED STATES
string iso3166TwoLetterCode = ipAddress.Iso3166TwoLetterCode(); // return value: US
}
you can then try something like this to change the master:
protected void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "NewMasterSite.master";
}
It sounds like you're not redirecting to another site, just sending them to a page with a querystring like "language=en". If so, then you need to get it with Request.QueryString and append it to a base master page name.
Related
My session key is a const string variable. See below.
On first Page Load, I add a string to the session using this key. I also indicate to KeepAlive on the key on the first load and every PostBack. However, on PostBack, I notice that the key is no longer in the session.
I found that to fix this, I simply have to remove "const" from the variable, and everything works fine.
Can someone explain and provide any educational resources on why this is happening.
private const string ENTITY_KEY = "c335a928-72ac-4403-b5f8-418f1e5ac1ec";
public string CurrentEntity
{
get { WebClientSession.Current[ENTITY_KEY] as string); }
set { WebClientSession.Current.AddTransient(ENTITY_KEY, value); }
}
protected void Page_Load(object sender, System.EventArgs e)
{
string key = (string)Request["Id"] + "";
CurrentEntity = Mapper.Lookup.FindById(key);
WebClientSession.Current.KeepAlive(ENTITY_KEY);
}
private void _bindGrid()
{
...
// CurrentEntity is null here on PostBack. Good on first load.
...
}
I am not sure what WebClientSession is but the HttpSessionState will work with const. There is no reason why it should not work. Here is the proof that it will work:
private const string ENTITY_KEY = "c335a928-72ac-4403-b5f8-418f1e5ac1ec";
protected void Page_Load(object sender, EventArgs e) {
if( !this.IsPostBack ) {
Session.Add( "ENTITY_KEY", ENTITY_KEY );
}
}
protected void Button1_Click(object sender, EventArgs e) {
string s = Session[ "ENTITY_KEY" ].ToString();
}
I simply added a button to my form. In the load method if the page is being requested I added a const variable's contents to the Session. In the click handler of the button, which is the form being posted, I access it from the Session and it is there.
So why is it not working for you?
There are 2 possible reasons:
Reason 1
The issue is in your WebClientSession class. I do not know the details of that class so cannot say what the issue is.
Reason 2
Session is stored in memory on the server. So, if this site is deployed on a farm, it is possible that the server which served the page initially added the ENTITY_KEY to Session. But when the page is posted back on the button click, another server serves the request. This server may not have the ENTITY_KEY in its memory since it is possible it has never served that page yet. In a web farm, you would want to use another source to store session related data such as a database or a file etc.
i use asp login control in web application (ASP.NET 4). if user in admin role i want redirect to admin page.
i use this code, but not working:
protected void baseLogin1_LoggingIn(object sender, LoginCancelEventArgs e)
{
if (Page.User.Identity.IsAuthenticated && Roles.IsUserInRole(Page.User.Identity.Name, "Admin"))
{
Page.Response.Redirect("admin/Default.aspx");
}
}
please help me.
LoggingInEvent is raised before the user is authenticated. So the first part of your condition is always false. Move you logics under LoggedIn event.
Try this one:
protected void baseLogin1_LoggedIn(object sender, EventArgs e)
{
if (Context.User.Identity.IsAuthenticated && Context.User.IsInRole("Admin"))
{
Context.Response.Redirect("admin/Default.aspx");
}
}
Use the LoggedIn event: Event description is here
You should really be using Server.Transfer("~/admin/Default.aspx"); as it is a little more efficient (less round trips).
If the page needs to preserve the query string for a bookmark or it is important to preseve the correct URL in the browser then Response.Redirect() is needed but be aware of the extra bandwidth cost.
I am trying to add a condition for a hyperlink that I have in my page.
Instead of just using a particular link like: Tutorial I want to display different pages for different users. For example, if the user is logged in as Admin, they will be presented with different link than regular users.
I have modified my hyperlink as: <a onclick="displayTutorial_Click">Tutorial</a>
and added this code:
protected void displayTutorial_Click(object sender, EventArgs e)
{
// figure out user information
userinfo = (UserInfo)Session["UserInfo"];
if (userinfo.user == "Admin")
System.Diagnostics.Process.Start("help/AdminTutorial.html");
else
System.Diagnostics.Process.Start("help/UserTutorial.html");
}
But this didn't work. Can anyone please help me to figure out how I can make the Tutorial link work properly? Thank you a lot in advance!!!
The onclick attribute on your anchor tag is going to call a client-side function. (This is what you would use if you wanted to call a javascript function when the link is clicked.)
What you want is a server-side control, like the LinkButton:
<asp:LinkButton ID="lnkTutorial" runat="server" Text="Tutorial" OnClick="displayTutorial_Click"/>
This has an OnClick attribute that will call the method in your code behind.
Looking further into your code, it looks like you're just trying to open a different tutorial based on access level of the user. You don't need an event handler for this at all. A far better approach would be to just set the end point of your LinkButton control in the code behind.
protected void Page_Load(object sender, EventArgs e)
{
userinfo = (UserInfo)Session["UserInfo"];
if (userinfo.user == "Admin")
{
lnkTutorial.PostBackUrl = "help/AdminTutorial.html";
}
else
{
lnkTutorial.PostBackUrl = "help/UserTutorial.html";
}
}
Really, it would be best to check that you actually have a user first.
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserInfo"] != null && ((UserInfo)Session["UserInfo"]).user == "Admin")
{
lnkTutorial.PostBackUrl = "help/AdminTutorial.html";
}
else
{
lnkTutorial.PostBackUrl = "help/UserTutorial.html";
}
}
Wow, you have a huge misunderstanding how asp.net works.
This line of code
System.Diagnostics.Process.Start("help/AdminTutorial.html");
Will not redirect a admin user to a new site, but start a new process on the server (usually a browser, IE) and load the site. That is for sure not what you want.
A very easy solution would be to change the href attribute of the link in you page_load method.
Your aspx code:
Tutorial
Your codebehind / cs code of page_load:
...
if (userinfo.user == "Admin")
{
myLink.Attributes["href"] = "help/AdminTutorial.html";
}
else
{
myLink.Attributes["href"] = "help/otherSite.html";
}
...
Don't forget to check the Admin rights again on "AdminTutorial.html" to "prevent" hacking.
this may help you.
In .cs page,
//Declare a string
public string usertypeurl = "";
//check who is the user
//place your code to check who is the user
//if it is admin
usertypeurl = "help/AdminTutorial.html";
//if it is other
usertypeurl = "help/UserTutorial.html";
In .aspx age pass this variabe
<a href='<%=usertypeurl%>'>Tutorial</a>
I think my tittle is inaccurate.
When a user clicks on a button I need it to do this:
Response.Redirect("Login.aspx?userid=XX");
How can I get the "userid?" from ?userid. so I can show a page. Like doing "?page=3" and show page 3, in the same page or something.
The Button code is: (just if you need it)
protected void LoginButton_Click(object sender, EventArgs e)
{
Response.Redirect("Login.aspx");
}
Thanks a lot! Sorry if I didn't ask it good, and sorry for the bad English.
Use Request.QueryString:
First Page Sends them another page with their user id in the url:
Response.Redirect("AfterLogIn.aspx?userid=23");
You then Read it using the below code:
var g = Request.QueryString["userid"] //this value should be 23 now
You could then use this g variable to do any amount of custom things (Hide panels, show controls, etc.)
You can do something like this
protected void LoginButton_Click(object sender, EventArgs e)
{
var id = // whatever userid
Response.Redirect("Login.aspx?userid="+ id);
}
and in the pageload of Login page
var userid = Request.QueryString["userid"];
ASP.NET State Management will explain further.
Hope this helps
I have created a small windows application for login a web application called ebridge. Its our company's internal website. I have written this code in my button click event to login this site:
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://app.ebridge-solutions.com/ebridge/3.0/Default.aspx?user=Ebridge&password=test&filecabinet=E Group");
}
Its login successfully. but my issue is that I need to redirect some other page i.e. (https://s2.ebridge-solutions.com/ebridge/3.0/retrieve/retrieve.aspx) not home.aspx page after login. Have u any idea or code to overcome this issue. Any help will be appreciated.
The best way (and probably the only way) is to add some logic/code to the page (https://app.ebridge-solutions.com/ebridge/3.0/Default.aspx) itself.
i.e.
from winform
System.Diagnostics.Process.Start("https://app.ebridge-solutions.com/ebridge/3.0/Default.aspx?user=Ebridge&password=test&filecabinet=E Group&redirect=true");
notice i have added redirect=true at the end of the querystring.
on the default.aspx Page_Load
string redirect= Request.QueryString["redirect"];
string redirect will act as a flag. if it's true, and if the login is successful. page will redirect to destined URL.
sample code for (default.aspx):
protected void Page_Load(object sender, EventArgs e)
{
string redirect= Request.QueryString["redirect"];
string user= Request.QueryString["user"];
string password= Request.QueryString["password"];
if (authorizeUserAndReturnStatus(user,password)&&redirect=="true") //assuming authorizing return bool, indicating the status of login (true or false)
{
Response.Redirect("https://s2.ebridge-solutions.com/ebridge/3.0/retrieve/retrieve.aspx");
}
}
If you're not using the Login control, but something else, why not a simple Response.Redirect?
private void button1_Click(object sender, EventArgs e)
{
Response.Redirect("https://app.ebridge-solutions.com/ebridge/3.0/Default.aspx?user=Ebridge&password=test&filecabinet=E Group");
}
UPDATE
This is for a Webform (ASP.NET).