简体   繁体   中英

Display authentication page on app built-in c# WebBrowser component when try to get UserCredential

I used the following code snippet to retrieve the UserCredential before.

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(...);

It will launch default browser and display the authentication page when first authentication.

Now I want to add a WebBrowser component in my WPF application, and I would like to show the authentication page on my built-in WebBrowser component, but I can't find a way to implement it.

Any help would be much appreciated.

This is not supported by the Google .net client library at this time. Currently GoogleWebAuthorizationBroker.AuthorizeAsync only opens a new browser there is no way to get the auth URI to embed it in your own webbrowser component.

There is an example of a windows phone UserControl here I have not tested it personally though. It might help

<UserControl x:Class="Google.Apis.Auth.OAuth2.WebAuthenticationBrokerUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    mc:Ignorable="d"
    x:ClassModifier="internal"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480">

    <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}" 
          HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <phone:WebBrowser Name="browser"
                          IsScriptEnabled="True"
                          Navigating="OnBrowserNavigating"
                          NavigationFailed="OnBrowserNavigationFailed"
                          Navigated="OnBrowserNavigated" />
        <ProgressBar Name="Loader" Height="10" Margin="10" VerticalAlignment="Top" IsIndeterminate="True" />
    </Grid>
</UserControl>

The source code for the client library can be found on GitHub it is an open source project so you are welcome to add it to the project.

there is also an issue request for this currently. GoogleWebAuthorizationBroker in embedded browser?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM