作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个应用程序,我有一个使用Owin Token Bearer
的后端服务器Azure API
应用程序。我正在尝试登录到我的后端并像在我的网络客户端中一样取回 token 。我可以成功登录,但响应消息中没有 token 。我有这个代码
string _url = ServerPath.Path + "/token";
HttpResponseMessage message = await _url.PostUrlEncodedAsync(
new {
UserName = Username,
Password = Password,
grant_type = "password"
}
);
任何帮助将不胜感激。
最佳答案
您使用的身份验证方法返回标准 OAuth 2.0 token response 。 token 位于HttpResponseMessage
中你回来了,Flurl 的 ReceiveJson
可以帮你把它拔出来:
var response = await _url
.PostUrlEncodedAsync(new
{
UserName = Username,
Password = Password,
grant_type = "password"
})
.ReceiveJson();
var token = (string)response.access_token;
请注意,我使用了 ReceiveJson
的非通用版本在这里,它返回 dynamic
。您可以通过创建一个与整个响应正文的 JSON 结构匹配的类(称为 TokenResponse
)来使其更加健壮,使用 ReceiveJson<TokenResponse>()
,并有一个强类型响应对象可供提取 access_token
出。
关于azure - 如何从 Flurl 获取 Access_Token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45643948/
我是一名优秀的程序员,十分优秀!