我为 Web API 设置了 Cookie 身份验证和基于 token 的身份验证。 成功登录后,我正在加载 SPA,这当然会向服务器发出多个 API 调用。
但是我怎样才能在进行这些调用之前获得 token 呢?我不确定该怎么做,需要一些建议。
最佳答案
我相信您需要调用 /Token/
端点来获取 token
您可以从 MS page 获取详细信息
从那个页面:
您首先需要在 url 上注册一个用户:
http://localhost:port/api/Account/Register
body 像
{
"UserName": "Alice",
"Password": "password123",
"ConfirmPassword": "password123"
}
您需要执行 POST 操作才能获取 token 。将 Content-Type header 更改为 application/x-www-form-urlencoded
。将请求正文替换为以下内容:
grant_type=password&username={userName}&password={password}
你应该得到这样的回应
{
"access_token":"boQtj0SCGz2GFGz[...]",
"token_type":"bearer",
"expires_in":1209599,
"userName":"Alice",
".issued":"Mon, 14 Oct 2013 06:53:32 GMT",
".expires":"Mon, 28 Oct 2013 06:53:32 GMT"
}
关于c# - 使用表单例份验证登录后如何在身份中获取 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24039627/