1313using System . Net . Http . Headers ;
1414using System . Text ;
1515using System . Threading . Tasks ;
16- using System . Web ;
17- using System . Windows . Forms ;
1816
1917namespace ConverterLibrary
2018{
@@ -44,7 +42,6 @@ public class Converter : PSCmdlet
4442 public string Token = "" ;
4543 private DeploymentTemplate template ;
4644 private JObject workflowTemplateReference ;
47- private AuthenticationContext ac = new AuthenticationContext ( Constants . AuthString ) ;
4845
4946
5047 public Converter ( )
@@ -61,66 +58,19 @@ protected override void ProcessRecord()
6158 {
6259 if ( String . IsNullOrEmpty ( Token ) )
6360 {
61+ AuthenticationContext ac = new AuthenticationContext ( Constants . AuthString , true ) ;
62+ var ar = ac . AcquireToken ( Constants . ResourceUrl , Constants . ClientId , new Uri ( Constants . RedirectUrl ) , PromptBehavior . Always ) ;
6463
65- var uri = String . Format ( "https://login.microsoftonline.com/common/oauth2/authorize?response_type=code&client_id={0}&redirect_uri={1}&resource={2}&prompt=consent" ,
66- Constants . ClientId , HttpUtility . UrlEncode ( Constants . RedirectUrl ) , HttpUtility . UrlEncode ( Constants . ResourceUrl ) ) ;
67- WriteVerbose ( uri ) ;
68- WebBrowser browser = new WebBrowser ( ) ;
69- browser . ScrollBarsEnabled = true ;
70- browser . Width = 800 ;
71- browser . Height = 840 ;
72- browser . ScriptErrorsSuppressed = true ;
73- browser . Url = new Uri ( uri ) ;
74-
75- Form form = new Form ( ) ;
76- browser . DocumentCompleted += Browser_DocumentCompleted ;
77- form . Controls . Add ( browser ) ;
78- form . AutoSize = true ;
79- form . ShowIcon = false ;
80- form . AutoSizeMode = AutoSizeMode . GrowOnly ;
81- form . Width = 810 ;
82- form . Height = 840 ;
83- form . ShowDialog ( ) ;
84-
85- // Token = ar.AccessToken;
86- }
87- WriteVerbose ( "Retrieved Token" ) ;
88- var result = ConvertWithToken ( SubscriptionId , ResourceGroup , LogicApp , Token ) . Result ;
89- WriteObject ( result . ToString ( ) ) ;
90- }
91-
92- private void Browser_DocumentCompleted ( object sender , WebBrowserDocumentCompletedEventArgs e )
93- {
94- if ( e . Url . AbsoluteUri . Contains ( "code=" ) )
95- {
9664
97- var queryDictionary = System . Web . HttpUtility . ParseQueryString ( e . Url . Query ) ;
98- var code = queryDictionary [ 0 ] ;
99- WriteVerbose ( "fullURL: " + e . Url . AbsoluteUri ) ;
100- WriteVerbose ( "code: " + code ) ;
65+ Token = ar . AccessToken ;
10166
102- using ( var client = new HttpClient ( ) )
103- {
104- IEnumerable < KeyValuePair < string , string > > authorizeParameters = new [ ] {
105- new KeyValuePair < string , string > ( "client_id" , Constants . ClientId ) ,
106- new KeyValuePair < string , string > ( "code" , code ) ,
107- new KeyValuePair < string , string > ( "grant_type" , "authorization_code" ) ,
108- new KeyValuePair < string , string > ( "redirect_uri" , Constants . RedirectUrl )
109- } ;
110- var response = client . PostAsync ( Constants . AuthUrl , new FormUrlEncodedContent ( authorizeParameters ) ) . Result ;
111- WriteVerbose ( response . Content . ReadAsStringAsync ( ) . Result ) ;
112- }
113- ( ( WebBrowser ) sender ) . FindForm ( ) . Close ( ) ;
114-
115- }
116- else if ( e . Url . AbsoluteUri . Contains ( "error=" ) )
117- {
118- WriteVerbose ( "error: " + e . Url . AbsoluteUri ) ;
119- ( ( WebBrowser ) sender ) . FindForm ( ) . Close ( ) ;
67+ WriteVerbose ( "Retrieved Token: " + Token ) ;
12068 }
121-
69+ var result = ConvertWithToken ( SubscriptionId , ResourceGroup , LogicApp , Token ) . Result ;
70+ WriteObject ( result . ToString ( ) ) ;
12271 }
12372
73+
12474 public async Task < JObject > ConvertWithToken ( string subscriptionId , string resourceGroup , string logicAppName , string bearerToken )
12575 {
12676 SubscriptionId = subscriptionId ;
0 commit comments