I'd like to redirect user to the main page after successful login, so I don't want him to have a back button or be able to get to the login page again.
I have redirected user to the login page with something like this:
if (isAuthenticated)
{
MainPage = new General.Pages.MainPage();
}
else
{
MainPage = new Account.Pages.Login();
}
I'm using this code for successful login, which doesn't work and gives an exception:
await Navigation.PopAsync(false);
await Navigation.PushAsync(new General.Pages.MainPage());
The given exception is:
System.InvalidOperationException: PopAsync is not supported globally on Android, please use a NavigationPage.
And no need to mention that the mentioned NavigationPage doesn't work neither.
I just figured it out, you can change MainPage at any point using this code:
App.Current.MainPage = new General.Pages.MainPage();
I leave this post to be, if it can possibly help other users.
There are 2 ways you could approach this.
Set you App.MainPage initially in the constructor of App.cs as your home page. Then in your home page's OnAppearing event or if it has a ViewModel then its Initialising method check if user is authenticated. If yes load his data. If not the show your Login page as a Modal. Once login is successful, you can pop the modal of login page and load the data for the home page. Also if required you can handle the back button event in the login page to not allow the user to go out of the modal page.
Drawback - The home page would be visible for a second before the login modal shows up.
In the constructor of App.cs check if the user is authenticated. If user is authenticated then show home page by setting it as the MainPage. Else set the MainPage as login page. Once login is successful again set the MainPage.
Drawback - Checking isAuthenticated in the constructor might make the initial load of the application seem slower.
And for the error you encounter is because you did not push any pages into the navigation stack. Hence your pop wont work.
var firstPage = isAuthenticated ? new General.Pages.MainPage() : new Account.Pages.Login();
MainPage = new NavigationPage(firstPage);
If you will use
mentioned NavigationPage
properly, PopAsync will work.
Related
I have to fix one issue in our application that, If child window is opened and user clicks on Browser back button i need to redirect to page asking username and password.
we have 400 aspx pages so just i need to modify in master page. I have code like below
function initPage(){
checkback();
}
This function is written in external javascript file and used in Master Page.
checkback function contains code like below
if (document.forms[0].cprotection.value=='1')
{
document.forms[0].pagecode.value=0;
document.forms[0].act.value='backpressed';
document.forms[0].submit()
}
The above code is working fine for parent window but not if i open child window.
backpressed is keyword am using to check in class file to redirect to page asking username and password. Please help me out in fixing this issue. Thanks in advance
You could add a variable to the session when the child window is loaded and if the user clicks back you would check the session to see if the variable that the child window added to session is nothing inside whichever pages load function and if it is then load normally, if its not redirect to the login page.
I have two pages, a login page and a page1. The user cannot directly navigate to page1 as it contains following code for the pageload event. The user is redirected to the login page.
if (Session["role"] == null)
{
Response.Write("Redirect Not Working");
Response.Redirect("loginpage.aspx");
}
When the user clicks logout on pag1, he/she is redirected to the login page after setting Session["role"]=null. Now on the login page, if the user clicks on the browser back button, he/she is able to navigate to page1. Only in this case Response.Redirect("loginpage.aspx"); in pageload event does not work. Why does it not work? How can I make it work, or how can I prevent the user from accessing page1 in this scenario?
I have been helpless and closed last time by asking it a different way code to detect browser back button click for any(all) browser
Edit In response to answers: The code against the logout button is
protected void btnLogOut_Click(object sender, EventArgs e)
{
Session["role"] = null;
Session.Abandon();
Response.Redirect("login.aspx");
}
The page you're seeing on back may just be a cached version.
The simplest way might be, instead of using response redirect, echo a meta refresh. You need to make sure the session is clear too.
Session.Abandon();
Response.Write("<meta http-equiv='refresh' content='0';URL='loginpage.aspx'>");
Response.End();
If a user hits back they'll hit that page again and be bounced to the URL you want them at. Nothing stopping them from hitting back quickly more than once or choosing Page1 from the history drop down and getting a cached version.
this should definitely work,check your Session["role"],I think its never null
at logout do this
Session.Abandon();
'pageoad is not working' in that case the reason for the page executing doesn't affect the page cycle, the Load event always fires when the page is executed.
So, if the Page_Load doesn't run sometimes, it's because the page is cached and doesn't execute on the server. The page can be cached in the browser, in a router somewhere along the way, or on the server using server side page caching.
If you haven't enabled server side page caching for the page, it's cached in the browser or in the network. You can use cache settings to try to elliminate this:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
This will keep the page from being cached in normal circumstances. (Check also that your browser isn't in offline mode, then it will use anything in the cache regardless of it's cacheability settings.)
Can you try something like this
if (Session["role"] == null)
{
Response.Write("Redirect Not Working");
Response.Redirect("~/loginpage.aspx");
}
MAKE sure to reset the Session["role"] = null at time of logout because this value will persist during web session
It sounds to me like you need to remove the Session["role"] value and set it back to null. When the user logs out I don't think that you are clearing your session values so when they browse back your page load still thinks that they have a valid logged in session.
An easy way to test if this is the case is to put a break point inside the if block past where you check to see Session["role"] == null. If you never hit that breakpoint you know that role is not null and they are still technically "logged in".
I am using C# to get FB tokens. So once I redirect user to this link:
https://graph.facebook.com/oauth/authorize?........
it opens a new page instead a popup (FB javascript SDK, which I am trying to avoid).
There is 'display=popup' in that query, which doesnt work. I also tried to 'window.open(...),
but some browsers block such popups with blockers...
Is there a way to open an original Facebook popup, and then go back to C# code behind to do all the magic..?
Have a separate page for managing Facebook login. Open the same from a main page with something like this
window.open(""../Helper/FBLoginMgr.aspx?login=fb", "_blank","height=900,width=800,menubar=0,resizable=1,scrollbars=1,status=0,titlebar=0,toolbar=0");
in FBLoginMgr.aspx in Page load event you could do this:
if (Request["login"] != null)
{
//First time
url = "https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope=offline_access,read_stream,user_activities";
Response.Redirect(string.Format(url, app_id, redirect_url));
}
if (Request["code"] != null)
{
//Second time
url = "https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}";
..
The initial querystring parameter login=fb is just to indicate to the Login mgr that it has just been launched and need to initiate Facebook authorization. The second Request["code"] is a parameter posted by Facebook upon authorization by the user.
Once the access token is retrieved, set a hidden variable to indicate that the job is done. Then in the aspx page you could have something like this:
$(document).ready(function () {
alert("Time to say good bye");
if (document.getElementById('<%=amidone.ClientID %>').value == "yes") {
window.parent.opener.doneWithLogin();//Indicate to the parent page
window.close();
}
});
I have an asp registration page using a custom asp:CreateUserWizard.
Once the registration is completed successfully (RegisterUser_CreatedUser for example) I want to redirect the user to another page, be it a welcome screen, etc... (using Response.Redirect(URL); I guess), but I also want to, some how, popup a new window with the login page.
Is it possible to popup a screen from an external url using this method, or is there another way I should go about it?
I did try creating a custom button which calls this js function for registration:
function redirectAfterRegister() {
Page_ClientValidate();
if (Page_IsValid) {
window.open('/Account/Login.aspx?UserCreated=True');
$('#CreateUserButton').click();
}
return false;
}
This popup works because its called off a click, but the problem with this is the popup is always called even if the creation of the user was unsuccessful - which is wrong.
Any help is highly appreciated.
The problem is that popups only work when a user actually clicks in external sites. This prevents spammers from popping up ads all the time. Once another function is called after the click it is considered unfriendly and therefore to allowed externally.
I think it best to let the user know the registration was successful and give them navigation options from there. If anything, at least its user friendly that way, without confusion.
The asp:CompleteWizardStep can be used to redirect after successful registration, and provide extra navigation where needed.
Good Luck, and let me know if you find an alternate solution.
Why not use the CreateUserWizard.ContinueDestinationPageUrl property to go to your welcome page. You can then place your javascript to open a new window in the onload event of the Body element.
i have one asp.net project. In this i used session for logged user. If i open a popup window and waited upto session expire. Then i got one error page that describing about the session expiration and redirect to the login page. but the login page open in the same popup window. My requirement is to open the login page in Main window. What changes that i will make for that?
if (Session["IsSessionValid"] != null)
{
//Some lines of codes
}
else
{
Response.Redirect("SessionError.aspx");
}
As far as i know i think we can use javascript to load the parent window with a new url
Write a function for the popup window.onclose event
In that function, you can refer the parent window from which you have opened a popup,
window.opener.location=url //replace the url of ur wish