【问题标题】:How to Consume Secured Web Api from C# MVC and AngularJS如何从 C# MVC 和 AngularJS 使用 Secured Web Api
【发布时间】:2015-02-20 23:58:19
【问题描述】:

如何从其他应用程序 C# MVC 和 AngularJS 使用 Secured Web Api。

[Authorize(Users = "Steve,Mike")]
public class EmployeeController : ApiController
{
    MyDB db = new MyDB();

    public IEnumerable<EmployeeViewModel> GetAllEmployee()
    {
            return db.Employee.Select(item => new EmployeeViewModel { EmpID = item.EmpID, Name = item.Name, Region = item.Region }).ToList();
    }
}

在没有来自 AngularJS 的 Secure WebApi 的情况下,我可以获得以下代码

var ft = searchText.toLowerCase();
                $http.get('/api/Employee/GetAllEmployee').success(function (largeLoad) {
                    data = largeLoad.filter(function (item) {
                        return JSON.stringify(item).toLowerCase().indexOf(ft) != -1;
                    });
                    $scope.setPagingData(data, page, pageSize);
                });

非常感谢您的帮助...

【问题讨论】:

  • 您在 MVC 项目中使用什么类型的身份验证?
  • 假设客户端在您发出请求时已通过身份验证和授权,您是否尝试在$http.config 对象上设置withCredentials 属性?这看起来像$http({ withCredentials: true }).get('/api/Employee/GetAllEmployee') 等等。
  • 在我们提供帮助之前,我们确实需要了解 API 中是如何处理身份验证的。

标签: angularjs asp.net-mvc-4 asp.net-web-api


【解决方案1】:

您想使用哪种 Secured Web Api? 你可以考虑 oAuth

【讨论】:

  • 请将此作为对问题的评论。这不是答案
  • 更不用说这在 ASP.NET 的上下文中没有意义
  • @devqon 目前不可能...如果声誉低于 50 则无法添加评论...谢谢
  • @CallumLinington 为什么没有意义?它可以将 oAuth 与 AngularJs 和 web api 一起使用...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-27
  • 1970-01-01
  • 2019-02-20
  • 2021-10-27
  • 1970-01-01
  • 2018-06-18
相关资源
最近更新 更多