我使用 Microsoft ASP.NET MVC 4 ApiController
制作了一个简单的 RESTful Web 服务(目前仅支持 GET)。 .
现在我正在寻找为服务实现授权系统的正确方法。我知道我不想使用内置 FormsAuthentication
,因为我不希望所有使用我的 WS 的应用程序都有一个唯一的登录页面;此外,这打破了 RESTful 范式,强制重定向并且没有通过正确的 401
通知客户端。状态码。
所以我禁用了 FormsAuthentication
从我的 web.config
中删除以下几行:
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
并添加以下内容:
<modules runAllManagedModulesForAllRequests="true">
<remove name="FormsAuthentication" />
</modules>
我已经有
ApiController
用于管理用户登录,它基本上根据我的数据库检查凭据并返回用户或 401
如果凭据无效。我读过我必须实现
Membership
和 Authorization
API,但我发现没有任何东西可以帮助我从头开始做。你有什么想法?我是否需要管理数据库上的 cookie 和 authcodes 或者是否有与
FormsAuthentication
类似的类?对我有用吗?
最佳答案
你可以写一个 custom message handler来处理身份验证。 Dominick Baier 写的非常好 series of blog posts深入研究细节。
关于asp.net-mvc - Microsoft MVC 4、APIController 和适当的 RESTful 登录 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9908115/