- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Background: I'm trying to use the Forge three legged authentication to get the user BIM 360 projects and documents.
I followed the LearnForge guide to create a .NET core web server, and added an endpoint that redirects to the OauthUrl (instead of only returning it as string), so that a client can simply call that endpoint to begin the authentication process.
In order to display the login window (and retrieve the cookie needed for further API requests), I want to use the WebView2 control inside a small WPF window.
I correctly installed the WebView2 runtime (just in case somebody asks).
I tried to build a simple stand-alone client and everything is fine, the app connects to my server and then the Autodesk login appears.
Inserting this logic inside a Revit plugin, though, results in a blank WebView2 widget:
I'm still learning c#/.NET, and I'm a newbie when it comes to WPF, did I miss something?
This is the minimum code to reproduce the issue
Command class
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace TestAutodeskLoginPlugin {
[TransactionAttribute(TransactionMode.Manual)]
public class TestAdskLoginCmd : IExternalCommand {
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) {
var loginDialog = new LoginWindow();
loginDialog.ShowDialog();
//do something with loginDialog.Cookie;
return Result.Succeeded;
}
}
}
The window Xaml
<Window x:Class="TestAutodeskLoginPlugin.LoginWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
mc:Ignorable="d"
Title="Autodesk Login" Height="450" Width="400">
<DockPanel>
<TextBlock Margin="10" DockPanel.Dock="Top">Please log in to BIM 360</TextBlock>
<wv2:WebView2 Name="webView" Source="http://localhost:3000/api/forge/oauth/login"/>
</DockPanel>
</Window>
The window logic (to get the cookie from webview2)
using Microsoft.Web.WebView2.Core;
using System.Windows;
namespace TestAutodeskLoginPlugin {
/// <summary>
/// Window that shows the Autodesk Bim360 login
/// </summary>
public partial class LoginWindow : Window {
private const string FORGE_COOKIE = "ForgeApp";
public string Cookie { get; set; }
public LoginWindow() {
InitializeComponent();
webView.NavigationCompleted += GetForgeCookies;
}
private async void GetForgeCookies(
object sender, CoreWebView2NavigationCompletedEventArgs args
) {
if(args.IsSuccess && webView.Source.Host == "http://localhost:3000") {
CoreWebView2 cwv = webView.CoreWebView2;
var cookies = await cwv.CookieManager.GetCookiesAsync(cwv.Source);
var forgeCookie = cookies.Find(c => c.Name == FORGE_COOKIE);
if(forgeCookie != default) {
Cookie = forgeCookie.Value;
Close();
}
}
}
}
}
Solved! Go to Solution.