c# - 使用表单例份验证登录后如何在身份中获取 token

标签 c# access-token asp.net-identity

我为 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/

相关文章:

c# - PropertyChanged 时 INotifyPropertyChanged

c# - MySql gridview删除命令

c# - HttpClient系统.Net.Http.HttpRequestException : Response status code does not indicate success: 401 (Unauthorized)

c# - 如何在 Web API 2 自托管应用程序中通过 Unity 注册 IAuthenticationManager?

c# - 手动编写 ASP.Net Identity 现有数据库脚本

c# - 如何使用 ASP.Net Core Identity 从登录用户检索 Facebook 个人资料图片?

c# - EWS 中 ItemView 的最大尺寸是多少?

c# - 弹出 View 时是否有 iOS 覆盖?

python - 获取 Soundcloud 桌面应用程序的访问 token ?

rest - 从 groovy 脚本中提取 token 的值