microsoft-authentication-library-for-dotnet icon indicating copy to clipboard operation
microsoft-authentication-library-for-dotnet copied to clipboard

Unable to find implementation of microsoft login for windows form applications

Open sfm2222 opened this issue 7 years ago • 11 comments

#documentation doesn't exist

Unable to find the implementation of Microsoft login for windows form applications.

How can I integrate Microsoft account login in C# windows application? I have tried going through the documentation but all the steps are for either web or ASP.NET core.

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/microsoft-logins?view=aspnetcore-2.2

I just want to login into my windows application using Microsoft account and get skype username.

sfm2222 avatar Jan 24 '19 14:01 sfm2222

Have a look at our wiki, particularly at the Interactive flow. MSAL will pop-up a WinForms panel that hosts a browser for your users to log in. When they login, you get given a access token that you can use to access protected APIs, like the Graph.

I am not sure how you'd find the skype username though.

bgavrilMS avatar Jan 25 '19 17:01 bgavrilMS

Can you provide a little more explicit guidance where winforms is documented in the wiki please? I can get wpf to work but not winforms, and can't find an example. I have tried with and without the following:- AcquireTokenByUsernamePassword(), AcquireTokenInteractive(), WithParentActivityOrWindow(form.Handle).

and WithParentActivityOrWindow(new WindowInteropHelper(form).Handle) gives the error "cannot convert from 'System.Windows.Forms.Form' to 'System.Windows.Window'"

pritchie99 avatar Jun 27 '19 20:06 pritchie99

WithParentActivityOrWindow(form) should work since your form is a Window. We don't take the handle object directly.

MarkZuber avatar Jun 27 '19 20:06 MarkZuber

I am not sure my Form is a "Window" as per the error at the end. My form is a subclass of System.Windows.Forms.Form.

No doubt this will be me not getting these Guids aligned once again, however at the moment it just "hangs" without raising an exception, and without showing a UI.

pritchie99 avatar Jun 27 '19 21:06 pritchie99

@pritchie99 : do you want to share a small repro solution (attach a zip file) ?

jmprieur avatar Jun 28 '19 09:06 jmprieur

You don't need to use WithParentActivityOrWindow(form) except if you want to pop-up browser to be centered on a window. Please try without it first.

A small repro would be helpful.

bgavrilMS avatar Jun 28 '19 10:06 bgavrilMS

Thank you all - File|New has worked with UI shown so presumably the case is related to artefacts from my old Winforms app.

pritchie99 avatar Jun 28 '19 19:06 pritchie99

Ran into the same here and was breaking my head over it for hours. I was using an (old-style) Windows Forms with .NET Framework application to make the call and the login screen never appeared. Until the light bulb above my head went on when I read in the Wiki that you need to call it on the UI thread as it will spawn a window to log in. I was using .Wait() to go from a synchronous button click to an asynchronous method where I would run these commands, thus away from the UI thread, thus the UI never showed up. Solution for me was to simply change the signature of the click handler to: private async void SignInButton_Click(object sender, RoutedEventArgs e) and get rid of the Wait() and Result. Works like a charm now.

KoenZomers avatar Jan 17 '20 14:01 KoenZomers

Hi,

I am facing the same issue of unable to get microsoft login in windows form applications. Can someone please provide the steps on how to proceed on this issue?

KrishnaAgrawal5 avatar Apr 14 '20 07:04 KrishnaAgrawal5

@pritchie99 Could you please let me know the steps you performed I am facing the same issue

KrishnaAgrawal5 avatar Apr 14 '20 08:04 KrishnaAgrawal5

@KrishnaAgrawal5 - I answered on the other thread you opened. Let's continue the conversation there.

bgavrilMS avatar Apr 14 '20 10:04 bgavrilMS