How to clear Cookies in WebView? - vb.net

How do you clear the cookies from a web-view in visual studio 2015.
The cookies persist through the code below and even through an application restart.
Dim cookieManager = httpBaseProtocolFilter.CookieManager
Dim cookieCollection = cookieManager.GetCookies(New Uri("https://www.example.com"))
For Each cook As HttpCookie In cookieCollection
cookieManager.DeleteCookie(cook)
Next

The title of this question does not specify the question. I see two questions of which I may have an answer for one. The question mark at the end of the following quote leads me to believe that you are wondering how to fix the fact that the next user is already logged in:
Navigate to a page and ask a user to login
Dim req As HttpRequestMessage = New HttpRequestMessage()
req.RequestUri = New Uri("https://example.com/login")
_WebView.NavigateWithHttpRequestMessage(req)
They do a process and then they close the application
Now when the next user opens the application they are already logged in?!
When the first user closes the application it is an event. Write code which resets the login state to false when the first user closes the application.

You should be able to clear the cache using WebView.ClearTemporaryWebDataAsync.. Some people have reported though that this isn't working for them.
The "old" way of handling this is adding a query string parameter, like a timestamp, to the browser. This usually allows the bypass the caching. So instead of navigating to example.com/login, you navigate to example.com/login?stamp={DateTime.Now.Ticks}

Related

How to read an arbitrary folder in a Universal app?

I'm trying to learn to write Universal apps, and I'm starting out by trying to recreate another project I wrote in WinForms. I need to be able to read from log files in an arbitrary folder, and as I understand it I need to get the users permissions to access the folder. I should then store an access token so I can re-read that folder in future. By reading around I've managed to cobble together the following code:
Friend Async Function GetLogFolder() As Task(Of StorageFolder)
Dim myLogFolder As StorageFolder
If ApplicationData.Current.LocalSettings.Values.ContainsKey("LogFolder") Then
Dim sToken As String = ApplicationData.Current.LocalSettings.Values("LogFolder")
myLogFolder = StorageApplicationPermissions.FutureAccessList.GetFileAsync(sToken)
Else
Dim myFolderPicker As FolderPicker = New FolderPicker
myFolderPicker.FileTypeFilter.Add("*")
myLogFolder = Await myFolderPicker.PickSingleFolderAsync
Dim sToken As String = StorageApplicationPermissions.FutureAccessList.Add(myLogFolder)
ApplicationData.Current.LocalSettings.Values.Add("LogFolder", sToken)
End If
Return myLogFolder
End Function
But it doesn't seem to work. At this stage I have a form containing just a TextBlock and Button. Clicking the Button calls a method that will parse all the *.log files in a given folder. The first thing it does is:
Dim myFolder As StorageFolder = GetLogFolder.Result
When the code runs, and I click the button, I get a folder browser dialog shown, but then everything freezes and I have to switch to Visual Studio and hit stop. I've probably made some silly error, but I can't figure out what it is.
Any help would be much appreciated.
The problem is very likely not caused by the file access code itself, but by the way you use the asynchronous API.
Because the GetLogFolder method returns a Task of StorageFolder, you will need to await the result instead of getting it using the Result property. The reason is that the async/await pattern allows you to do I/O work on a separate thread but return control back to the UI thread when finished. What you are doing here is to call the GetLogFolder method, in which you let the user choose a folder using FolderPicker. Here is the problem - the user is presented with a folder picker while the control returns to your code and you query the Result property of the Task returned by the GetLogFolder method. Querying the Result property causes the UI thread to stop and wait until the Task is finished to get the result. Unfortunately, when the user picks the folder, the control wants to return to the UI thread to continue executing the rest of the GetLogFolder method and we have a deadlock. Result property stopped the UI thread to wait for the Task result and the Task waits for the UI thread to become available. Neither can continue so the app completely freezes.
The solution is quite simple - using the async / await keywords. You can read up more about them in VB.NET here with a clear example.
In your case the first step would be to make the button's Click handler method async and then replace the code inside by the following:
Dim myFolder As StorageFolder = Await GetLogFolder

UWP WebView - Issues with webserver login when displaying webserver content

I need to display a few of webpages from a remote Apache webserver for the IoT Core app I'm developing. I've used the WebView class method .Navigate() for non-protected pages and It works really neat. However, for my project, I need to login first into the webserver (username + password) and then retrieve the info from the pages, but I don't know how to do it using the WebView class. I'm clueless.
I found a solution that uses the WebBrowser class Navigate(), sending the credentials as a 64 base encoded string, but the Navigate() of WebView only allows 1 argument, the URI, and nothing else. I can't seem to find WebBrowser class neither.
I've already tried to embed the username/password into the URI but It's not working properly and I'm aware It's not a good idea to do it so.
Is it possible to achieve this using WebView?Any suggestions/ideas?
Any help appreciated
Edit: I've found a solution that works well for my problem, I'm posting it in case it can be of help to someone with similar issues.
Uri req_uri = new Uri(uri_list[i]);
HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
filter.ServerCredential = new PasswordCredential(req_uri.ToString(), "username", "password");
HttpCookieCollection cookiejar = filter.CookieManager.GetCookies(req_uri);
if (cookiejar.Count > 0)
{
foreach (HttpCookie cookie in cookiejar)
{
filter.CookieManager.SetCookie(cookie);
}
}
Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient(filter);
Windows.Web.Http.HttpRequestMessage http_req = new Windows.Web.Http.HttpRequestMessage();
http_req.Method = Windows.Web.Http.HttpMethod.Get;
http_req.RequestUri = req_uri;
var clientResponse = client.GetAsync(req_uri);
webView.NavigateWithHttpRequestMessage(http_req);
http_req.Dispose();
client.Dispose();
You might me able to use the NavigateWithHttpRequestMessage which would allow you to navigate to a page using a HttpRequestMessage.
You would still need to authenticate out-of-band, using HttpClient, obtain the cookies and authentication headers, then use them when you build your HttpRequestMessage.
Here's a StackOverflow that should help with that
If your remote web server supports HTTP Basic authentication you can pass the credentials in the URL like so:
https://Username:Password#www.example.com/index.html

Ionic 2 app: remember user on the device

I have an Ionic App and I'd like to put a "Remember me" checkbox in the login page, so that once the user has logged in he'll be logged in forever (unless he logouts), even if he close and re-opens the application, just as the Facebook app does.
Is there a way to do that in Ionic 2?
Thanks!
You can use localstorage to save some data for your own verification/logic handling.It's html5 supported and easy to use. The downside would be it won't be secure and it might be erased by system when there's low memory or clearing of cache.
HTML(Upon button press, but u can set it to your own preference):
<button secondary (click)="addLocalStorage()">Done</button>
In your controller:
import {Storage, LocalStorage} from 'ionic-angular';
constructor(navController, local) {
this.navController = navController;
this.local = new Storage(LocalStorage);
}
addLocalStorage(){
this.local.set("didTutorial","true");
//set the doneTutorial to be true
}
Explanation:
At the constructor , we create an new localstorage object called 'local'.
To call it, we use 'this.local' and 'set' is the method to store it.
Set(Key,Value)
In the example code above, i use 'didTutorial' as the key and 'true' as the value.
To retrieve it, you can retrieve it at this page or at any other page.
Just remember to import Storage and LocalStorage and declare a new localstorage object in the constructor.(same as above)
The code to retrieve is :
var value = localStorage.getItem('didTutorial');
getitem(Key,Value)
You can use storage and sqlStorage to save user data. And then check it for saved user.
Refer :-
https://www.thepolyglotdeveloper.com/2015/12/use-sqlite-in-ionic-2-instead-of-local-storage/
Hope this helps!
localStorage works fine but I confirm the drawbacks that #Gene talks about. It is not reliable, especially on iPhones that do not have much free storage
I use ionic 1 so I cannot try this solution, but you might want to check this out : https://ionicframework.com/docs/native/native-storage/
; could be more reliable!

Session lost while on PythonAnywhere Web2Py but NOT locally

I use a session variable to pass some info with a redirect:
session.OrigText = XML(str(OrigText))
redirect(URL('SearchResultsOrigText'))
It arrives at the new URL / page / view - SearchResultsOrigText - and works Ok.
But when from that new URL - SearchResultsOrigText- I navigate away (and it doesn't matter where I go from this new page), when returning with the 'back" button of the browser, the session.OrigText is now empty (showing as 'None').
This behaviour is happening only on PA and not locally.
I do not use session.forget anywhere in my code.
Trying to pass the 'html heavy' content in the OrigText as a dictionary variable (not a session variable) gets me into another interesting issue...Python Anywhere says "Something's wrong 502- Back End".
(Silent failing ?) This happens on PythonAnywhere but NOT locally as well.
Sanitizing this var doesn't help...
But let's focus on the first question...
Why is the session variable lost after 1.redirect and 2.leaving the new page / view - when hosted on PythonAnywhere and NOT locally ?
Thanks
Flask sessions, by default use cookies, so it's possible that somewhere in your settings you have a setting for what domain to set the cookies on and that is not set correctly. It's also possible that you haven't set a secret key for sessions.
I faced the same problem recently in PythonAnywhere. I solved it by deleting my domain's cookies from Firefox. I still don't know how they were messed up, though.
Make sure there is no "session.forget(response)" in your code or framework code along the way.

vb.net - WebBrowser1.Navigate tries to download JSON file

I am trying to navigate to a website with json data using the webbrowser controls but it keeps prompting me to download the file instead of properly navigating to the page as firefox would.
I have tried doing a regular navigate:
frmBrowser.WebBrowser1.Navigate("http://us.wowarmory.com/auctionhouse/money.json")
As well as editing the header content type with many different types:
frmBrowser.WebBrowser1.Navigate("http://us.wowarmory.com/auctionhouse/money.json", "", Nothing, "Content-Type: text/plain" & vbCrLf)
But cant seem to get it working.. Keep in mind I need to use the webbrowser to navigate as you have to be logged in to access this file.
Edit: Also, manually editing my computers registry won't work as I need to distribute this program.
Edit2: Just wanted to add that this code would work if it were the same session, but since it webclient creates a new session it doesn't work
Dim oWeb As New System.Net.WebClient()
oWeb.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
Dim bytArguments As Byte() = System.Text.Encoding.ASCII.GetBytes(params)
Dim bytRetData As Byte() = oWeb.UploadData(url, "POST", bytArguments)
Return System.Text.Encoding.ASCII.GetString(bytRetData)
If your application will allow it, just rename it to money.json.html or something similar. Will download without a problem.