简体   繁体   English

如何在 UWP 桌面应用程序中使用 oauth2 授权代码授予流程登录

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

I am trying to enable single sigon in my UWP app.我正在尝试在我的 UWP 应用程序中启用单 sigon。

I tried using WebAuthenticationBroker and able to get the AuthCode我尝试使用 WebAuthenticationBroker 并能够获取 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对于访问令牌调用,我使用的是常规 httpclient 库

 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.我从 postman 尝试过,我也得到了同样的错误。

Is that possible to get access token with 0auth2.0 authorization grant flow in windows desktop apps based on UWP platform?是否可以在基于 UWP 平台的 windows 桌面应用程序中获得具有 0auth2.0 授权授予流程的访问令牌? Does it expect the request to come from the actual host uri in case of fetching access token in exchange of authorization code?在获取访问令牌以交换授权码的情况下,它是否期望请求来自实际的主机 uri?

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OAuth2:使用 C# 的授权代码授予流程 - OAuth2: Authorization Code Grant Flow with C# 如何使用OWIN中间件在Web Api中实现OAuth2 Authorization_Code流? - How do I implement an OAuth2 Authorization_Code Flow in Web Api using OWIN Middleware? 我想在 UWP 应用中打开浏览器(用于 oAuth2 授权) - I Want to open a browser within an UWP app (for oAuth2 grant) 无需用户干预即可进行 OAuth 2 授权代码授予/流程 - Make OAuth 2 Authorization Code Grant/Flow without user intervention WebForm 用授权码替换 OAUTH2 隐式流 - WebForm replace OAUTH2 implicit flow with Authorization Code 使用客户端凭据流进行Swashbuckle OAuth2授权 - Swashbuckle OAuth2 Authorization with Client Credentials Flow 有没有办法在 c# 桌面应用程序中使用 oauth2 登录 Xero api? - Is there a way to signin Xero api using oauth2 in c# desktop application? 如何使用具有来自 C# 可执行文件的“authentication_code”授权流类型的 OAuth 2.0 进行身份验证? - How to authenticate using OAuth 2.0 with `authentication_code` grant flow type from a C# executable? 如何在UWP应用中访问oauth2令牌响应 - How to access oauth2 token response in a UWP app 如何使用 authentication_code 在 C# 中获取 Oauth2 不记名令牌? - How to obtain an Oauth2 bearer token in C# using authorization_code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM