asp.net - 如何为 Web API GET 请求指定用户名和密码

标签 asp.net asp.net-mvc asp.net-mvc-4 asp.net-web-api forms-authentication

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/

相关文章:

asp.net - 用于网站的数据驱动开源温度计

css - ValidatorCalloutExtender 无法正确呈现 CSS

asp.net - 手动升级后将新的 ASP.NET Web 优化框架添加到 MVC4 项目

asp.net-mvc-4 - NuGet 包还原失败

c# - Ninject 和 asp.net MVC4

Asp.net Json Post 请求

c# - ASP .Net Core Dto 和 Controller 到 typescript 类和接口(interface)

asp.net-mvc - ServiceStack 和 MVC4 未接线

asp.net-mvc - 使用 ASP.NET MVC 发布 Ajax 数组?

ajax - 为什么从 ASP.NET 下载时文件不同(使用 Blob 进行 AJAX 下载)