kenakamu / UCWA2.0-CS

C# library for UCWA 2.0
MIT License
24 stars 13 forks source link

One item does not work on UWP which breaks the whole library #3

Closed mjbedford2017 closed 7 years ago

mjbedford2017 commented 7 years ago

Hello, I am trying to use your library on UWP to access SFB via my app. This is a great library from what I can tell (when I run the demo app in non-uwp). However, UWP does not allow me to reference the library: Microsoft.IdentityModel.Clients.ActiveDirectory.Platform

Furthermore and related, the class UserPasswordCredential does not exist anymore either. From what I read, this is because Microsoft no longer wants to store passwords. While I understand this approach for most scenarios, there is certainly still a case for it. Aside from that, it seems the library will work just fine on UWP. However, as you know, this breaks everything because I cannot get the token, etc... without the password.

In days of research, I found this beauty. It is a hack or workaround to the problem. Basically, instead of using AcquireTokenAsync and passing in a UserPasswordCredential, I am able to use this code and successfully acquire a string which is the token. The issue is, I do not know how to inject this into your library the correct way so everything else downstream still works. In other words, I know that you typically get a bit more than just a string of the token but rather, an AuthenticationResult. While the token is part of that AuthenticationResult, it is not the only part of it. Any ideas how to correct this issue so the library will work on UWP OR inject my workaround into your library?

Thanks!

 ` public static async Task<string> GetAzureAdTokenStringAsync(string ResourceId)
    {
        if (ResourceId.StartsWith("http"))
        {
            ResourceId = Helpers.ReduceUriToProtoAndHost(ResourceId);
        }

        //create the collection of values to send to the POST

        var vals = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>("resource", ResourceId),
            new KeyValuePair<string, string>("username", Username),
            new KeyValuePair<string, string>("password", Password),
            new KeyValuePair<string, string>("grant_type", "password"),
            new KeyValuePair<string, string>("client_id", ClientId)
        };

        var url = string.Format(AadInstance, Tenant);

        // ... Use HttpClient.
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Cache-Control", "no-cache");

            HttpContent content = new FormUrlEncodedContent(vals);

            HttpResponseMessage hrm = await client.PostAsync(url, content);

            // ... Read the string.
            var responseContent = await hrm.Content.ReadAsStringAsync();

            // ... Display the result.
            if (hrm.IsOk() && hrm.IsJson())
            {
                var resultDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseContent);

                return resultDictionary["access_token"];

            }
        }
        return string.Empty;
    }`
kenakamu commented 7 years ago

Thanks for using this. Yes this does work with any platform including UWP and Xamarin.

Basically you use ADAL v3 to acquire token by using user login and adal takes care the rest.

However I will write sample and put it to this repo as it could be easier. Please give me a day to prepare.

mjbedford2017 commented 7 years ago

While I do appreciate you writing a sample app that targets UWP and others may benefit from it, my issue may just be my fault if you say it already works with UWP. I just know that UWP does not allow me to reference Microsoft.IdentityModel.Clients.ActiveDirectory.Platform which is referenced in your test app. So, to re-write a test app in UWP, I could not figure out how to get past that.

I do appreciate you writing a new sample and can certainly wait a day. In the meantime, I will keep playing around to see if I can figure it out too.

Thanks again! Look forward to your sample.

kenakamu commented 7 years ago

not perfect sample but i just uploaded UWP sample. Please let me know how it helps you.