C# Uwp app webbrowser control cannot get device location - c#

I am developing a web view universal windows application. In fact my mobile ready website will be shown within a web view or webbrowser control in windows universal application..
Using the website directly from Edge in mobile (or chrome in desktop etc) the app notifies the user to allow using location as it should (HTML Geolocation API ).
However in my app using web browser control this is not happening. It is not able to run the script or something.
I have given grant access to user's location in the app but still no action is taken.
What else can I do in order to get user's location within an app that uses webbrowser ?

I found the solution.
You have to fire the PermissionRequested event. And in this event you have to execute the Allow method of the WebViewPermissionRequestedEventArgs object. Something like this:
private void webView_PermissionRequested(WebView sender, WebViewPermissionRequestedEventArgs args)
{
if (args.PermissionRequest.PermissionType == WebViewPermissionType.Geolocation)
args.PermissionRequest.Allow();
}

Related

Automated login from C# Using Post in WPF

I've been trying to write an app to use OAUTH method with pInterest to Upload new pins automatically and have been trying to find the best way of doing this.
So Far i've got a working example going where in my WPF app in C# i can get the login details using a RestAPI Get Method to get the login URL for the pInterest user and once logged it my app will hit tab twice then enter to give permission from pInterest.
The Method i've got working involves -
Using .NET 4.6.2 or later getting WebView UI WPF controls from nuget
Using RestAPI Get Method to get the URL of the app accept page
Using the output of step 2 in a webview component to let IE login and display the App Accept Page ( IE having username / password set up already)
OnNavigateComplete once the URL has "/api.pinterest.com/oauth" using SendKeys.SendWait from Winforms to send Tab, Tab , Enter
Once Navigate completes grab the URL if its got "state" & "code" in the URL then close the form.
I'm looking to see if there is a better way of doing this i've tried CefSharp but the GetAuthorizeCreditials does not fire off when i implement my own IResponseHandler as its a Post Submit button and i've tried to use javascript code from C# passed in via cefSharp but i couldnt get it to work

My xamarin application will not redirect after logging in to facebook

I am new to xamarin forms, I've done plenty of searching on SO and google and cannot solve this problem that has had me stuck for a few days now.
I want the users of my mobile application to be able to login with Facebook.
I've followed the code example on https://github.com/xamarin/Xamarin.Auth/.
I have also setup my application of developers.facebook.com
I am debugging using an Android emulator via VS2017 with Xamarin.
I am writing a X platform app, so I have a PCL, Android and iOS project.
When I click my facebook login button, the facebook web UI displays as expected, when I log in, control does not return to the app. It just sits on the facebook newsfeed for my facebook profile in an embedded browser. The OAuth2Authenticator.Completed event never fires. If I press the back button on my android device, it then DOES fire, but e.IsAuthenticated is false.
This is the code to launch the Facebook login
var auth = new OAuth2Authenticator(FaceBookAppId, "",
new Uri("https://m.facebook.com/dialog/oauth/"),
new
Uri("https://www.facebook.com/connect/login_success.html"));
auth.Completed += Auth_Completed;
auth.Error += Auth_Error;
var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
presenter.Login(auth);
I have a breakpoint sat on the event handler, but it never hits.
async void Auth_Completed(object sender, AuthenticatorCompletedEventArgs e)
{
if(e.IsAuthenticated)
{
//do some magic
}
}
Not sure what I have done wrong. Please help.
After a few more days of trying to solve this, it is quite clear that the Xamarin.Auth package is out of date.
I found that by deploying the release to an actual device, the login worked fine, but it did not function on an emulator.
I decided to go down the "manual" web based approach which is detailed in facebook dev docs here
https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
much more simple solution.

Facebook Desktop Auth pop up Browser

I'm writing a desktop app in C# that has Facebook integration and I'm trying to figure out how to do authentication/login. I have thought of two different approaches:
1. Popup default browser
The user probably is logged into Facebook on their default browser.
Code: System.Diagnostics.Process.Start("http://www.facebook.com/...");
Issues/Questions: How do I control the window location and size (e.g. not show address bar when it starts)? Can I destroy the process after login is complete or even close the window (won't most browsers prompt for window closing if done from javascript?)?
2. Popup specific browser
If I lookup the default browser, I can pass command-line flags to the browser. "..\chrome.exe" --app=http://www.facebook.com/...
Questions: How do I set the window size/location? How do I close the process after login-complete (assuming I know when the login is complete)?
Is there a better way to do this?
very usefull resource in this situation is http://facebooksdk.net/.
Earlier, there was a sample project for desktop application on facebooksdk.net, but it was removed. You can see it here https://github.com/MarkAureliy/facebook-winforms-sample-master
This project is straight for your needs

.NET 4.0 Web browser control and msIsSiteMode javascript error

I have an application that connects to www.Jango.com and using a web browser control. However when using the .NET browser control I constantly receive a script error of “Unable to get value of the property 'msIsSideMode': object is null or undefined”, and therefore the rest of the site does not load.
This can be reproduced by creating a simple windows forms application, adding a webbrowser control and navigating to jango.com
As far as I can tell, the web browser control renders a website based on the version of IE installed on your machine. On my machine I have IE9 installed. The method msIsSiteMode appears to be an IE method that tells weather the current page was launched as a pinned site. Since jango.com is very JavaScript based, this null value causes the website to stop functioning correctly. However navigating to jango.com in Internet Explorer works just fine and no errors are produced.
Is anyone aware of a way to work around this, such as having my application set the value? Have the web browser control render as a different version. Any suggestions at all, I am open to anything, except changing the version of IE on my machine as this is not an option.
Set the ScriptErrorsSuppressed property to True on the WebBrowser control.
EDIT: One more alternative
Try this on the Form that hosts the web user control:
using System.Runtime.InteropServices;
using System.Security.Permissions;
[ComVisibleAttribute(true)]
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.ObjectForScripting = this;
}
...
}
Read here for more detailed information.
I see similar problem in WebBrower Control with IE9 installed on my machine. In my case, I have two windows forms with WebBrowser Controls. One of those is used to load the pop up from the other.I get the script error on pop up one.its only with IE9

"Unspecified error" from javascript in my Silverlight application

I have a Silverlight application written in C# that utilizes the Bing Maps SDK. In this application I have a page with some javascript functions to expose functions within the application. The application registers the Bing MapControl in the MainPage's constructor with:
HtmlPage.RegisterScriptableObject("Page", new MapControl(this.BingMap));
In the javascript function, I call the methods of the application like so:
<script type="text/javascript">
function ClearMap() {
var control = document.getElementById('silverlightControl');
control.Content.Page.ClearMap();
}
</script>
However, whenever I refer to the control.Content.Page property in a javascript function, I get an "Unspecified error". Even just setting it to a variable will cause the error, for example:
var page = control.Content.Page;
Here is the strange part. I am using this silverlight application from 2 other applications. One is a windows service, and the other a winform application. The winform embeds a visible WebBrowser into its form and uses that to call the javascript functions. When using the winform, everything works fine with no errors.
The error only occurs when calling these functions from the windows service. The service instantiates a WebBrowser object just like the winform application does, and initializes everything in the same way. The only difference is that it's running as a service, and the WebBrowser is not visible nor embedded in any forms. Could that fact possibly cause this error?
I currently have the winform application, the windows service, and the silverlight application's webservice all running on my own local machine.
UPDATE
After Gnostus' comments, I figured out that the main issue I'm having is that the "Page" object is not being created because the App class never gets initialized after navigating to the webservice from the windows service-based WebBrowser. The winform app and the service both do this the exact same way:
WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
WebBrowser.Navigate("http://localhost:56062/BingMapsWebServiceTestPage.aspx");
while (WebBrowser.ReadyState != WebBrowserReadyState.Complete)
System.Windows.Forms.Application.DoEvents();
I added some code to the MainPage's constructor where it calls HtmlPage.RegisterScriptableObject("Page", new MapControl(this.BingMap)); that will log some text when it gets hit. When I run the above code in the WebBrowser of the winform application, the following occurs:
The DocumentCompleted event gets fired twice (once by the browser and I assume once by the silverlight control)
The App() is intialized and the "Page" element successfully added in the MainPage's constructor
The above while loop completes
When I do the same from the windows service, it does #1 and #3, but not #2. Step 2 only gets hit when I call a javascript function that refers to the Page object, and only after it errors.
UPDATE 2
After messing around in a new winform application, I discovered that the silverlight control will fail to load upon calling Navigate() whenever the WebBrowser or its parent form is invisible. I found that it's possible to still call my javascript functions if the parent form is hidden AFTER the silverlight application has finished loading. Does anyone know a better way to force the silverlight app to load aside from flashing up a visible form? Is it even possible to show a form from a windows service, or am I going to have to rethink this whole thing as a console application?

Categories

Resources