ASP.NET MVC4 WebAPI 应用程序对 WebAPI 使用表单例份验证:
[Authorize]
public class EntityController : APIBase
{
public HttpResponseMessage Get(string id,
string _sidx = null,
string _sord = null,
uint _page = 1,
uint _rows = 100,
string _filters = null,
int? _dokumnr = null,
int? _vmnr = null,
string _isik = null,
uint _npage = 1,
bool _unpaid = false,
uint _layout = 0,
string _culture = null
)
{
....
这需要传递身份验证 cookie。 如何允许从浏览器 url 调用 Get() 方法。
用户名和密码可以在 URL 中使用
http:://user:pass@mysite.com/API/Entity/Customer
或作为查询字符串
http:://mysite.com/API/Entity/Customer?user=myuser&password=mypass
如何在 API Controller 中获取用户和密码并通过使用表单例份验证验证这些凭据来登录,就像 Authorize 属性一样?
或者是否有更好的方法从浏览器 url `调用此 API 方法?
最佳答案
首先,你应该使用 ViewModels,因为你的 Actions 有很多参数,出于安全原因,你不应该通过查询字符串传递用户名和密码。如果您有更具体的场景,您可以创建自己的 [Authorize]
过滤器,并将验证逻辑放入其中。您的身份验证信息将通过请求 header 发送到服务器。
关于asp.net - 如何为 Web API GET 请求指定用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34187801/