Share via

Get token without app restart

hokushin 1 Reputation point
2021-04-13T20:14:25.877+00:00

Hello folks!

I use the following login method in my WPF application.

  1. User click on WPF desktop app login button, the wpf form disappears, a web browser opens up.
  2. The user is redirected to the project web browser login form. At the same time WPF app closes down.
  3. If the user enters right credentials the web app generates token that the WPF app intercepts on startup in the method below:

protected override void OnStartup(StartupEventArgs e)
{
if (Environment.GetCommandLineArgs().Length > 1)
{
// here I get token using Environment.GetCommandLineArgs() function
var token = Environment.GetCommandLineArgs()[1];

My question is. is it possible to get the token without restarting WPF app?

Any help is appreciated!

Developer technologies | Windows Presentation Foundation

2 answers

Sort by: Most helpful
  1. hokushin 1 Reputation point
    2021-05-15T08:49:08.593+00:00
    1. app.exe is running
    2. browser starts another app.exe process like below
      start /c "app.exe" -token "XXX"
    3. running app.exe process should intercept the generated token "XXX" from the app.exe process launched by browser. just token.
      • app.exe is a single instance wpf app.

    Was this answer helpful?

    0 comments No comments

  2. hokushin 1 Reputation point
    2021-04-18T20:35:04.197+00:00
    1. Redirect to browser:
      public ICommand SignInCommand { get; }

    public LoginViewModel(){
    ...
    SignInCommand = new RelayCommand(SignIn);

    private void SignIn()
    {
    loginUrl.OpenLink();
    close app code

    ....
    public static void OpenLink(this string link)
    {
    var psi = new ProcessStartInfo
    {
    FileName = link,
    UseShellExecute = true
    };
    Process.Start(psi);
    }

    1. Getting token on start up in the App.xaml.cs.

    protected override void OnStartup(StartupEventArgs e)
    {
    ...
    base.OnStartup(e);

    if (Environment.GetCommandLineArgs().Length > 1)
    {
    GlobalSettings.Instance.AppToken = Environment.GetCommandLineArgs()[1];
    _dialogService?.Show(ViewName.MAIN);
    }
    else
    {
    _dialogService?.Show(ViewName.LOGIN);
    }

    1. Registry file (installer set it up) when a user clicks a link in the webapp the wpf app is started and get a token via Environment.GetCommandLineArgs() function.

    Windows Registry Editor Version 5.00

    [HKEY_CLASSES_ROOT\boseaudiodriver]
    @="URL:webapp"
    "URL Protocol"=""
    "UseOriginalUrlEncoding"=dword:00000001

    [HKEY_CLASSES_ROOT\webapp\DefaultIcon]
    @="\"C:\Program Files\MyWPFApp\MyWPFApp.exe\",1"

    [HKEY_CLASSES_ROOT\webapp\shell]

    [HKEY_CLASSES_ROOT\webapp\shell\open]

    [HKEY_CLASSES_ROOT\webapp\shell\open\command]
    @="\"C:\Program Files\MyWPFApp\MyWPFApp.exe\" \"--url=%1\""

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.