question

hokushin-7025 avatar image
0 Votes"
hokushin-7025 asked hokushin-7025 answered

Get token without app restart

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!

windows-wpf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@hokushin-7025
How do you make the user redirected to the Web Browser Login form?Could you show me the related code to analyze? Is it possible to pass token as parameter to the Web Browser page?

0 Votes 0 ·
hokushin-7025 avatar image
0 Votes"
hokushin-7025 answered hokushin-7025 commented
  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\""

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@hokushin-7025
How about saving your token in a file when your application exits? You can override OnExit in your App.xaml.cs like below:

   protected override void OnExit(ExitEventArgs e)
         {
             base.OnExit(e);
             File.WriteAllText(@".....\Token.txt", "Your Token");
         }

Then you can get token from Token.txt file in your web browser app.


0 Votes 0 ·

Appologies for have not been clear.
1. In my scenario, the web browser generates the token. the wpf app simply redirects a user to web page of the project.
2. then wpf app closes
3. A user logs in through the web browser. If credentials are right the web browser generates token.
If a user clicks a link (Launch Wpf App) on the web form the wpf app is launched and it gets token via
Environment.GetCommandLineArgs()[1]; command on StartUp event.
I want to get rid of step 2 in my scenario.
I want to intersept token without restarting (invoking onStartup event)

0 Votes 0 ·

@hokushin-7025
Is your Web browser a WPF WebBrowser project? Does web page of the project belong to WebBrowser project? Don't you want wpf to close?

0 Votes 0 ·
Show more comments
hokushin-7025 avatar image
0 Votes"
hokushin-7025 answered
  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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.