IdentityModel / IdentityModel.OidcClient.Samples

Apache License 2.0
296 stars 164 forks source link

Password autosave + suggest #131

Open AlbertoCe opened 2 years ago

AlbertoCe commented 2 years ago

Hi all, I'm trying to use webview2 to authenticate against IdentityServer 4 for my WPF app. I'm using OidcClient (from IdentityModel) with this implementation:

var options = new OidcClientOptions()
{
    Authority = App.ApplicationSettings.IdentityServerUrl,
    ClientId = App.ApplicationSettings.ClientId,
    Scope = App.ApplicationSettings.Scope,
    RedirectUri = App.ApplicationSettings.ClientRedirectUri,
    Browser = new EmbeddedBrowser(),
    LoadProfile = true,
    Policy = new Policy
    {
        RequireIdentityTokenSignature = false
    }
};

Using this as WebView2:

var semaphoreSlim = new SemaphoreSlim(0, 1);
var browserResult = new BrowserResult()
{
    ResultType = BrowserResultType.UserCancel
};

var signinWindow = new Window()
{
    Width = 800,
    Height = 600,
    Title = "Sign In",
    WindowStartupLocation = WindowStartupLocation.CenterScreen
};
signinWindow.Closing += (s, e) =>
{
    semaphoreSlim.Release();
};

var webView = new WebView2();
webView.NavigationStarting += async (s, e) =>
{
    if (IsBrowserNavigatingToRedirectUri(new Uri(e.Uri)))
    {
        e.Cancel = true;
        browserResult = new BrowserResult()
        {
            ResultType = BrowserResultType.Success,
            Response = new Uri(e.Uri).AbsoluteUri
        };

        semaphoreSlim.Release();
        signinWindow.Close();
    }
};

signinWindow.Content = webView;
signinWindow.Show();

webView.CreationProperties = new CoreWebView2CreationProperties(); 
var env = await CoreWebView2Environment.CreateAsync();

await webView.EnsureCoreWebView2Async(env);

webView.CoreWebView2.Settings.IsPasswordAutosaveEnabled = true;
webView.CoreWebView2.Settings.IsGeneralAutofillEnabled = true;

// Delete existing Cookies so previous logins won't remembered
//webView.CoreWebView2.CookieManager.DeleteAllCookies();
// Navigate
webView.CoreWebView2.Navigate(_options.StartUrl);

await semaphoreSlim.WaitAsync();

return browserResult;

Webview suggest me known username (used by this page) but does not ask for password change neither ask to save.

Any suggestion?

brockallen commented 2 years ago

Not sure -- isn't that a feature of the browser you use?

AlbertoCe commented 2 years ago

Hi @brockallen, probably you are right and it could depends on the webview2 control (I opened an issue on the control repository too). I tried the WpfWebView2 sample in your repository and there are no message to the user asking to save password. I'll do some more test today and I'll let you know.