使用 Web API 2 登录/身份验证

Login / authenticate with WebAPI2

我正在尝试通过登录保护我的 Web API 2 REST API。我被告知我应该为此使用 ASP.Net 身份,所以我得过且过。

我终于拼凑了一些使用 WebAPI 2 / OWIN / NHibernate 来创建 Web 服务的东西。模板带有一些默认 API 调用。使用这些,我能够在数据库中成功注册一个本地帐户。

现在 - 我不知道如何 "log in" 访问其他电话。这是像传入 headers 或数据这样的非常基本的事情吗?我在这个模板中没有看到任何似乎让用户使用本地帐户登录的功能。

原始源代码超过 350 行。我现在只是粘贴函数定义,但我可以根据需要进行扩展。

更新 我试过通过 JSON 传递用户名/密码之类的事情。这没有用。我真的不确定框架提供了什么作为登录方法。

' GET api/Account/UserInfo
<HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)>
<Route("UserInfo")>
Public Function GetUserInfo() As UserInfoViewModel

' POST api/Account/Logout
<Route("Logout")>
Public Function Logout() As IHttpActionResult


' GET api/Account/ManageInfo?returnUrl=%2F&generateState=true
<Route("ManageInfo")>
Public Async Function GetManageInfo(returnUrl As String, Optional generateState As Boolean = False) As Task(Of ManageInfoViewModel)


' POST api/Account/ChangePassword
<Route("ChangePassword")>
Public Async Function ChangePassword(model As ChangePasswordBindingModel) As Task(Of IHttpActionResult)


' POST api/Account/SetPassword
<Route("SetPassword")>
Public Async Function SetPassword(model As SetPasswordBindingModel) As Task(Of IHttpActionResult)


' POST api/Account/AddExternalLogin
<Route("AddExternalLogin")>
Public Async Function AddExternalLogin(model As AddExternalLoginBindingModel) As Task(Of IHttpActionResult)


' POST api/Account/RemoveLogin
<Route("RemoveLogin")>
Public Async Function RemoveLogin(model As RemoveLoginBindingModel) As Task(Of IHttpActionResult)


' GET api/Account/ExternalLogin
<OverrideAuthentication>
<HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)>
<AllowAnonymous>
<Route("ExternalLogin", Name := "ExternalLogin")>
Public Async Function GetExternalLogin(provider As String, Optional [error] As String = Nothing) As Task(Of IHttpActionResult)


' GET api/Account/ExternalLogins?returnUrl=%2F&generateState=true
<AllowAnonymous>
<Route("ExternalLogins")>
  Public Function GetExternalLogins(returnUrl As String, Optional generateState As Boolean = False) As IEnumerable(Of ExternalLoginViewModel)

' POST api/Account/Register
<AllowAnonymous>
<Route("Register")>
Public Async Function Register(model As RegisterBindingModel) As Task(Of IHttpActionResult)


' POST api/Account/RegisterExternal
<OverrideAuthentication>
<HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)>
<Route("RegisterExternal")>
Public Async Function RegisterExternal(model As RegisterExternalBindingModel) As Task(Of IHttpActionResult)

关于ASP.Net身份;你看过这个 link 以使用相应的 API: http://www.asp.net/identity/overview/getting-started/introduction-to-aspnet-identity?

我认为这个 link 或许可以帮助您理解 RESTful 服务中的身份验证原则:https://templth.wordpress.com/2015/01/05/implementing-authentication-with-tokens-for-restful-applications/

希望对你有帮助, 蒂埃里

一种技术是使用基本身份验证,其中凭据存储在 header 中。 Mixing Forms Authentication, Basic Authentication, and SimpleMembership. Although the article was written for SimpleMembership a follow-up article describes how it can also be implemented using ASP.NET Identity. Complete source code for these articles with example application can be found in the SimpleSecurity Project on CodePlex.

上的这篇文章对此技术进行了描述

刚刚在 ASP.NET Web API 2 中可用的另一种技术是不记名令牌的使用。 this article 中对不记名令牌的使用有很好的概述。

PluralSight 上有一门名为 Web API v2 Security 的优秀培训课程,其中涵盖了所有这些主题以及代码示例。您可以获得一个临时的 PluralSight 帐户来试用。

@Kevin 提供的资源很有帮助,但他的回答并没有直接回答问题。

简而言之:我显示代码的模板当前不允许本地登录。看来我必须设置自己的授权服务器,或使用外部 OAuth2 提供程序(如 Google 或 Facebook)。一旦我有更多时间,我将使用实际修复/代码更改来更新此 post,这将使它成为一个完全有效的实现。