I am creating an android application in xamarin. I am using a web view to display a website. After user login website create a cookie and should store in web view. There are two web view in app, One web view is displaying the pages and if there is any text box in the page that page is opened in second web view.
So now when user tries to login, second web view is opened(as login page contains text box), after user enter details and click next button, the second view is closed and next page is opened in first web view. After login a cookie is created and stored in web view and when user open the app next time its doesn't asks for login. This is what should happen.
The problem is, if the user enter details and after clicks the next button(next page is loading in first web view) and immediately quits the application then start app again then cookies does not exists and app asks for login again.
After login I am reading the cookie value on page finish event of webview and displaying in toast. If i quits the app after login, i gets the cookie value in toast but when i starts the app again the cookie doesn't exists anymore and it asks me for login again
public override void OnPageFinished (WebView view, string url)
{
try
{
if (view.Url == Urls.URL_INDEX)
{
var cookieManager = CookieManager.Instance;
if (cookieManager != null)
{
//getcookie string from the url
string cookie = cookieManager.GetCookie (view.Url);
if (!string.IsNullOrEmpty (cookie))
{
string[] cookies = cookie.Split (';');
foreach (var newcookie in cookies)
{
if (newcookie.Trim().StartsWith (Constants.COOKIE_NAME))
{
string cookieValue = newcookie.Substring (newcookie.IndexOf ('='));
Toast.MakeText(activity,cookieValue,ToastLength.Short).Show();
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine ("Exception in storing cookie in Home Activity : "+ex.Message);
Toast.MakeText (activity, "Exception : " + ex.Message,ToastLength.Long).Show();
}
}
I don't know why this is happening, please help.
The cookies are stored in RAM to get the best performance and synced every five minutes to the permanent storage. You'll need to manually force CookieSyncManager to sync the cookies in your OnPageFinished method so that they're still available when you start the application again. Refer to the CookieSyncManager documentation for more details.
Related
I am developing an android application where i have redirected to https://m.facebook.com/ inside a web-view.
What is required?
I want to check the login status, once login is successfully done and user is on home page i want to handle visibility of some views and redirect webview to another URL which is facebook video link.
What i have tried?
I have tried checking URLs in onLoadResource and found some URLs that we get on successful login and logout. But its still not enough as Facebook login can be done via different methods (i.e by number, by email, by already saved account etc).
To check Logout:
fun isFacebookLoggedOut(url: String?): Boolean {
return url?.startsWith("https://m.facebook.com/?stype=lo&jlou")!!
}
To check Login:
fun isFacebookLoggedIn(url: String?): Boolean {
if (url.equals("https://m.facebook.com/login/save-device/?login_source=login#_=_") || url.equals(
"https://m.facebook.com/login/save-device/cancel/?flow=interstitial_nux_retry&nux_source=regular_login"
) || url?.contains("https://m.facebook.com/login/device-based/validate-pin/?refid=")!!
|| url.startsWith("https://m.facebook.com/login/device-based/login/async/?") || url.startsWith(
"https://m.facebook.com/login/account_recovery/name_search/?flow=initiate_view&ls=initiate_view"
)
) {
return true
} else {
return false
}
}
This works in some cases and on some devices but there are scenarios when user log in with contact number, or saved account which goes through different phases like entering credentials then verifying any code if browser is new and so on.
Is there any method or proper way to track login status thats perfect for each scenario and each device?
Can somebody please help me out with this.
Any help will be appreciated
You can visit this link to have all the detailed information to implement the Facebook SDK and be able to login to Facebook throught your app.
I'm new with Xamarin form, my question is how can I check user is logged in for entire app, example every time when users go to a new page, it checks for authent. I tried successfully for every page check for auth but is there any another ways to do it? I did some research on internet and some said that i have to auth on the App.cs on OnStart() but the event is not raise when i go to the next page, it starts only when user open the app.
Here is my code, I'm using Google auth.
On the logged in page (HomePage):
public HomePage(NetworkAuthData networkAuthData)
{
if (string.IsNullOrEmpty(networkAuthData.Id))
{
//Always require user authentication
//Application.Current.MainPage.Navigation.PopAsync();
Application.Current.MainPage = new NavigationPage(new SocialLoginPage(oAuth2Service));
}
else
{
BindingContext = networkAuthData;
InitializeComponent();
}
}
It works but when i move these code to app.cs on OnStart() it just run once when opened the app.
It is my first time working with Xamarin, and need to show up a back button on the screen only when the URL of my WebView is different from the first one, so that the users can go back to the "home" page of the web site. If there is any function that gets called once the page is completely loaded so I can get the URL and compare with the previos one would be great. Thanks you!
You could get the url from the event webView.Navigated
webView.Navigated += WebView_Navigated;
private void WebView_Navigated(object sender, WebNavigatedEventArgs e)
{
var url = e.Url;
//...
}
After some back and forth I finally got this to work but I had to use version 0.2.0 because I followed the google guide presented in the Readme.
Anyway, Im struggling with handling what will happen when the oAuth token times out. Then it needs to open the browser again to log in or is there a background process available for this as it automatically redirects back to the app because the server remembers the user so there is no need for a new username/password input?
Im getting a refresh token like this :
if(mAuthService == null){
mAuthService = new AuthorizationService(context);
}
mAuthState.performActionWithFreshTokens(mAuthService, new AuthState.AuthStateAction() {
#Override public void execute(
String accessToken,
String idToken,
AuthorizationException ex) {
if (ex != null) {
return;
}
// Getting the access token...
}
});
Thats working fine but after the user is idle for some time it wont work. How to handle this properly?
Solution for my problem was this:
I changed to using offline_access for the token in the scope. Depending on the site/service you're login into if they accept it or not. For me it was accepted and will keep the user logged in for a long time and removes the need to re-login.
I have an android app with CordovaActivity which loads a login page for wordpress website using loadUrl("loginurl"), when the user enters his credentials for the first time a prompt asks for password remembering, however when the user closes the app and reopen it the password field is cleared and just got the username remembered.
when using the native android webview for the same site the password is saved correctly but I need to use cordova in my app, any idea how to make CordovaWebview remember wordpress login password?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// enable Cordova apps to be started in the background
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
moveTaskToBack(true);
}
LOGIN_URL = getString(R.string.site_url) + "/wp-login.php";
// Set by <content src="index.html" /> in config.xml
loadUrl(LOGIN_URL);
}
After 3 months I found a solution, for some reason when you use webview and load the WP login page, wordpress clears the password filed if you are already logged in instead of redirecting you to the home page, the solution was to not load the login page on application start up and just load the home page and put a login widget.