private BearerToken getDeviceCodeAuthentication(string endpoint, UserCodeObject deviceCode) { BearerToken bt = null; int graceTime = Int32.Parse(deviceCode.expires_in); bool deviceauthenticated = false; while ((graceTime > 0) && (deviceauthenticated == false)) { graceTime = graceTime - Int32.Parse(deviceCode.interval); WriteWarning(deviceCode.message); HttpClient client = new HttpClient(); String tokenEndpointUrl = "https://" + endpoint + "/Common/oauth2/token"; String clientid = "1b730954-1685-4b74-9bfd-dac224a7b894"; String Granttype = "device_code"; String result; Task <string> p; Dictionary <string, string> tokenEndpointGranttypeDict = new Dictionary <string, string>(); tokenEndpointGranttypeDict.Add("resource", "https://graph.microsoft.com"); tokenEndpointGranttypeDict.Add("grant_type", Granttype); tokenEndpointGranttypeDict.Add("client_id", clientid); tokenEndpointGranttypeDict.Add("code", deviceCode.device_code); Task <HttpResponseMessage> response = client.PostAsync(tokenEndpointUrl, new FormUrlEncodedContent(tokenEndpointGranttypeDict)); response.Wait(); var r = response.Result; p = r.Content.ReadAsStringAsync(); p.Wait(); result = p.Result; if (r.IsSuccessStatusCode) { bt = JsonConvert.DeserializeObject <BearerToken>(result); WriteVerbose("authentication successfull"); deviceauthenticated = true; } else { AuthError AE = JsonConvert.DeserializeObject <AuthError>(result); WriteVerbose("authen1tication failed - waiting " + graceTime); String badVerificationCode = "bad_verification_code"; if (AE.error.Equals(badVerificationCode)) { deviceCode = getDeviceCode(endpoint); } System.Threading.Thread.Sleep(Int32.Parse(deviceCode.interval) * 1000); } } return(bt); }
private BearerToken getGrantTypeCodeAuthentication(string endpoint) { // code authentication is currently not possible as we do not have a browser // support for the auth rendering. instead, we use "devicecode" similar to what // is implemented in the Azure az module. // I didn't find an official documentation for that procedure on Microsofts // website UserCodeObject deviceCode = getDeviceCode(endpoint); BearerToken bt = null; bt = getDeviceCodeAuthentication(endpoint, deviceCode); return(bt); }