Bug with SetUserAgent #366

Closed
opened 2026-01-29 16:37:44 +00:00 by claunia · 3 comments
Owner

Originally created by @DiefBell on GitHub (Jul 23, 2019).

Originally assigned to: @GregorBiswanger on GitHub.

Using the following code, which according to what I can find online, should but doesn't set the UserAgent header accordingly, and instead uses the default UserAgent string.

var mainWindow = await Electron.WindowManager.CreateWindowAsync(options); mainWindow.WebContents.Session.SetUserAgent("ModX"); mainWindow.OnReadyToShow += () => { mainWindow.Show(); };

Originally created by @DiefBell on GitHub (Jul 23, 2019). Originally assigned to: @GregorBiswanger on GitHub. Using the following code, which according to what I can find online, should but doesn't set the UserAgent header accordingly, and instead uses the default UserAgent string. `var mainWindow = await Electron.WindowManager.CreateWindowAsync(options); mainWindow.WebContents.Session.SetUserAgent("ModX"); mainWindow.OnReadyToShow += () => { mainWindow.Show(); };`
claunia added the question label 2026-01-29 16:37:44 +00:00
Author
Owner

@GregorBiswanger commented on GitHub (Jul 26, 2019):

The documentation from native electron says: "This doesn't affect existing WebContents, and each WebContents can use webContents.setUserAgent to override the session-wide user agent."
(see https://electronjs.org/docs/api/session).

Maybe that's your problem?

@GregorBiswanger commented on GitHub (Jul 26, 2019): The documentation from native electron says: _"This doesn't affect existing `WebContents`, and each `WebContents` can use `webContents.setUserAgent` to **override** the session-wide user agent."_ (see https://electronjs.org/docs/api/session). Maybe that's your problem?
Author
Owner

@DiefBell commented on GitHub (Jul 26, 2019):

Well I'd assume that this would effectively be me overriding the default
UserAgent for my main window?

On Fri, Jul 26, 2019 at 9:03 PM Gregor Biswanger notifications@github.com
wrote:

The documentation from native electron says: "This doesn't affect
existing WebContents, and each WebContents can use webContents.setUserAgent
to override the session-wide user agent."

(see https://electronjs.org/docs/api/session).

Maybe that's your problem?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/ElectronNET/Electron.NET/issues/295?email_source=notifications&email_token=ACYAM4THWI7V2N67TU56ZHTQBNKADA5CNFSM4IGHGDD2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD25SOFY#issuecomment-515581719,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACYAM4UDR75B34RMQUHCMGLQBNKADANCNFSM4IGHGDDQ
.

@DiefBell commented on GitHub (Jul 26, 2019): Well I'd assume that this would effectively be me overriding the default UserAgent for my main window? On Fri, Jul 26, 2019 at 9:03 PM Gregor Biswanger <notifications@github.com> wrote: > The documentation from native electron says: *"This doesn't affect > existing WebContents, and each WebContents can use webContents.setUserAgent > to override the session-wide user agent."* > (see https://electronjs.org/docs/api/session). > > Maybe that's your problem? > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub > <https://github.com/ElectronNET/Electron.NET/issues/295?email_source=notifications&email_token=ACYAM4THWI7V2N67TU56ZHTQBNKADA5CNFSM4IGHGDD2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD25SOFY#issuecomment-515581719>, > or mute the thread > <https://github.com/notifications/unsubscribe-auth/ACYAM4UDR75B34RMQUHCMGLQBNKADANCNFSM4IGHGDDQ> > . >
Author
Owner

@GregorBiswanger commented on GitHub (Jul 28, 2019):

Hi @MerrickKing,

I found the following example: https://stackoverflow.com/questions/35672602/how-to-set-electron-useragent

Here is the solution for change the UserAgent with Electron.NET.

Task.Run(async () =>
{
    var browserWindowOptions = new BrowserWindowOptions()
    {
        Show = false
    };

    var mainWindow = await Electron.WindowManager.CreateWindowAsync(browserWindowOptions);
    mainWindow.LoadURL("http://localhost:" + BridgeSettings.WebPort, new LoadURLOptions
    {
        UserAgent = "ModX",
    });

    mainWindow.OnReadyToShow += () => mainWindow.Show();
});

In your view you can see the new UserAgent with:

navigator.userAgent;

I hope I could help you. Have fun with Electron.NET! Happy coding...

@GregorBiswanger commented on GitHub (Jul 28, 2019): Hi @MerrickKing, I found the following example: https://stackoverflow.com/questions/35672602/how-to-set-electron-useragent Here is the solution for change the UserAgent with Electron.NET. ``` Task.Run(async () => { var browserWindowOptions = new BrowserWindowOptions() { Show = false }; var mainWindow = await Electron.WindowManager.CreateWindowAsync(browserWindowOptions); mainWindow.LoadURL("http://localhost:" + BridgeSettings.WebPort, new LoadURLOptions { UserAgent = "ModX", }); mainWindow.OnReadyToShow += () => mainWindow.Show(); }); ``` In your view you can see the new UserAgent with: `navigator.userAgent;` I hope I could help you. Have fun with Electron.NET! Happy coding...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#366