I have used a WebBrowser control in my windows phone application. Using webbrowser control i am logging into my application. On log-out i simply redirect the user to log-in page again and trying to clear the cache and cookies of the webbrowser control.
mybrowser.ClearCookiesAsync();
mybrowser.ClearInternetCacheAsync();
instead of showing log-in screen, it get the previous credentials and logged-in into the application. Can anyone please help me in clearing the cookies and cache of webbrowser conteol.
Complete code:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
App.AccessToken = "";
myBrowser.Navigate(new Uri(login URL));
}
void Navigating(object sender, NavigatingEventArgs e)
{
if (Login complete)
{
clearCahe();
}
}
public async void clearCahe()
{
await myBrowser.ClearCookiesAsync();
await mybrowser.ClearInternetCacheAsync();
}
async functions are hard to get use to. If you put a break point at
await myBrowser.ClearCookiesAsync(); // put a break point here then step-in (F11 on the keyboard)
You will see that will not actually await and will continue on its merry way.
To fix this you have to do something like this
using System.Threading.Task;
public async Task<bool> clearCahe()
{
await myBrowser.ClearCookiesAsync();
await mybrowser.ClearInternetCacheAsync();
return true;
}
and a calling function that also async
like so
private async void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
await clearCache();
// then navigate the page
}
Related
I have a main page : HomeVM.
When the app starts, I use PushModalAsync for RegistrationVM
In my RegistrationVM, I have a button for pushModalAsync -> LoginVM
When the user is connected, I want go back in the VM HomeVM, but impossible, with PopModalAsync, I come back to RegistrationVM.
I would like to close all popmodalasync.
I have tried this
Here in my app.xaml :
var mainPage = (Page)ViewFactory.CreatePage(typeof(HomeVM));
MainPage = new NavigationPage(mainPage)
{
BarBackgroundColor = (Color)Resources["PrimaryColor"],
BarTextColor = Color.White,
};
Navigation = MainPage.Navigation;
protected override void OnStart()
{
var registrationpage = (Page)ViewFactory.CreatePage(typeof(RegistrationVM));
MainPage.Navigation.PushModalAsync(registrationpage);
}
RegistrationVM :
public RegistrationVM()
{
Task.Run(async () => await ConnectionAPI());
}
async Task ConnectionAPI()
{
try
{
applicationContext.Device = mydevice;
await Navigation.PushModalAsync<LoginVM>(async (vm, p) => await vm.InitializeAsync(this));
}
catch (Exception e)
{
Log.Error(e, "Unhandled exception while loading touch points : {e}", e);
}
finally
{
}
}
LoginVM :
When the user click on "Login"
PreviousVM = RegistrationVM.
async Task Login()
{
try
{
Log.Information("Logging in");
applicationContext.User = employe;
await Navigation.PopModalAsync();
await Navigation.RemoveAsync(previousVM);
}
catch (Exception e)
{
Log.Error(e, "Unhandled exception during log in : {e}", e);
}
finally
{
}
}
I come back to Registration view
If you want to just pop all modal windows, you can should be able to keep calling PopModalAsync() until Application.Current.MainPage.Navigation.ModalStack.Count is 0, or you get an ArgumentOutofRangeException.
That said, you might want to consider using a different approach here. Most apps that require a login page don't want to have any other pages loaded while it's displayed. In other words, you don't want the user to be able to back out to your main page unless they are logged in, so you don't want anything else on the stack.
To do this, you can replace the Application.Current.MainPage with your login and/or registration page. When login is complete, you replace it with your app's main navigation page.
I'm using https://www.pujolsluis.com/google-client-plugin-for-xamarin/ for google sign in xamarin forms. Both Login and Logout methods work fine; but i have to hide the login page after successful login.After opening the app from second time onwards, logout method throws java.Lang.IlegalStateException<Timeout exceeded getting exception details> ,cannot logout.Active token is null.How to handle this exception? How to logout successfully from second time?
Login:
public IGoogleClientManager googleClientManager;
googleClientManager = CrossGoogleClient.Current;
private void google_btn_Clicked(object sender, EventArgs e)
{
if (CrossConnectivity.Current.IsConnected)
{
googleClientManager.LoginAsync();
googleClientManager.OnLogin += OnLoginCompleted;
// CrossGoogleClient.Current.SilentLoginAsync();
// var userToken = CrossGoogleClient.Current.ActiveToken;
}
else
{
DependencyService.Get<IToast>().LongAlert("Check Connection!");
}
}
public async void OnLoginCompleted(object s,
GoogleClientResultEventArgs<GoogleUser> loginEventArgs)
{
if (loginEventArgs.Data != null)
{
GoogleUser googleUser = loginEventArgs.Data;
string google_name = googleUser.Name;
string google_mail = googleUser.Email;
Uri google_img = googleUser.Picture;
googleClientManager.OnLogin -= OnLoginCompleted;
}
}
Logout:
public void Logout()
{
googleClientManager.OnLogout += OnLogoutCompleted;
googleClientManager.Logout(); // throws exception from secondtime after hiding loginpage
}
private void OnLogoutCompleted(object sender, EventArgs loginEventArgs)
{
googleClientManager.OnLogout -= OnLogoutCompleted;
}
You are getting this exception because you are trying to log out of a google client that is no longer connected as the message of the exception states.
Google Client Illegal State Exception Screenshot
To solve this you could do two things, fix the logic in your app to persist the logout state, so you don't try and log out when the user is not actually logged in anymore. Or you could enable the ActiveToken and add an if statement before trying to log out to verify if it's null or not you can do so by following the steps on the getting started guide on the project repo: https://github.com/CrossGeeks/GoogleClientPlugin/blob/master/GoogleClient/docs/GettingStarted.md
Activate ActiveToken Google Client Plugin Guide Screenshot
I am developing a windows 7 phone app where I want a particular page( A privacy policy which the user needs to accept ) to be displayed when the user uses the app for the first time in his phone. Can any one please tell me how to get this done.
Two approaches can be taken for this functionality. The first is to always start at the privacy policy page but override the OnNavigatedTo method to check if the policy has been accepted before. If it has, navigate to your "MainPage". Within the Mainpage, remove all backstack entries.
Privacy Policy Page
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// have the accepted the policy?
var settings = IsolatedStorageSettings.ApplicationSettings;
bool accepted;
if(settings.TryGetValue("PolicyAccepted", out accepted))
{
if (accepted)
{
NavigationService.Navigate(new Uri("MainPage.xaml", UriKind.Relative));
}
}
}
private void OnAcceptButtonClick(object sender, RoutedEventArgs routedEventArgs)
{
var settings = IsolatedStorageSettings.ApplicationSettings;
settings["PolicyAccepted"] = true;
settings.Save();
NavigationService.Navigate(new Uri("MainPage.xaml", UriKind.Relative));
}
Then in your MainPage, remove the backstack
protected override void OnNavigatedTo(NavigationEventArgs e)
{
while (NavigationService.CanGoBack) NavigationService.RemoveBackEntry();
}
The second approach is to is a UriMapper as described in this SO answer.
A possible answer to this question can be found here. As you want your page to open up only once I suggest IsolatedStorageSettings to save a boolean value if the user has accepted the policy or not.
I m my application I want to use Webbrowser which will be using Wifi,after the Webbrower opens the the link (task.URL) ,I want to check what is the response like whether the linked got opened or it failed.How do I do that means get the response ?
WebBrowserTask task = new WebBrowserTask();
task.URL = "https://www.goggle.com/";
task.Show();
kindly help
Thanks.
Usually, to catch this use NavigationFailed event as follow:
void WebPage_Loaded(object sender, RoutedEventArgs e)
{
this.webHome.Navigate(new Uri(www,UriKind.Absolute));
this.webHome.NavigationFailed += webHome_NavigationFailed;
}
void webHome_NavigationFailed(object sender, System.Windows.Navigation.NavigationFailedEventArgs e)
{
this.webHome.NavigateToString("No web page available.");
}
When i try to get uploaded filename from generic handler (upload.ashx) using session its ok, no problem. I can also use webmethod on samepage and uploadify works great, but Session["fileName"] is getting null. Is there anything wrong on my code? Do i only need to use generic handler to get filename?
[WebMethod(EnableSession = true)]
public void LoadPicture(HttpContext context)
{
try
{
HttpPostedFile file = context.Request.Files["Filedata"];
context.Session["fileName"] = file.FileName;
....................Some resize and save image codes.........
context.Response.Write("1");
}
catch (Exception ex)
{
context.Response.Write("0");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
using (_modelService = new ModelService())
{
ModelEntity _models = new ModelEntity();
......some codes....
_models.modelphoto = Session["fileName"].ToString();
_modelService.ModelAdd(_models);
}
}
Uploadify uses Flash. Flash doesn't send cookies. In ASP.NET sessions are tracked by cookies. So, no session with uploadify, sorry.