简体   繁体   中英

how to signin using oauth2 authorization Code Grant flow in UWP Desktop app

I am trying to enable single sigon in my UWP app.

I tried using WebAuthenticationBroker and able to get the AuthCode

var authenticationResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, requestUri, redirectUri);

The above code returns me the authorization code which i can use to exchange for access token when i make another call but it always gives me bad request error. For the access token call i am using regular httpclient library

 JObject jsonObject = new JObject();
 jsonObject["code"] = autorizationCode;
 jsonObject["grant_type"] = "authorization_code";
 jsonObject["redirect_uri"] = redirectUri.AbsoluteUri;
 var json = jsonObject.ToString();

 HttpStringContent requestBody = new HttpStringContent(json, UnicodeEncoding.Utf8, "application/json");
 var httpResponseMessage = await client.PostAsync(new Uri("https://<my app url>.com/oauth2/token"), requestBody);

I tried from postman and i get the same error too.

Is that possible to get access token with 0auth2.0 authorization grant flow in windows desktop apps based on UWP platform? Does it expect the request to come from the actual host uri in case of fetching access token in exchange of authorization code?

Some of the parameters needs to be sent as encoded and having below code solved the issue

List<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>();

postData.Add(new KeyValuePair<string, string>("code", autorizationCode));

var encodedReqBody = new FormUrlEncodedContent(postData)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM