Tag: asp.net web api

使用cookie的ASP.NET web api“记住我”function

我正在尝试在我的Web Api项目中实现“记住我”function。 我想要 : 用户登录时拥有“ 记住我”function。 保存cookie以保持用户始终登录,以便用户在访问网站时无需每次都输入用户名和密码 。 通过阅读上次登录时保存的cookie来签署用户。 我正在考虑的另一个问题是……我正在尝试使用JavaScript在用户选中“ 记住我”复选框时生成cookie 。 是否有可能做到这一点? 要么 我应该在AccountController实现RememberMe() ?? 另外:这是我在ApplicationOAuthProvider的代码。 public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var userManager = context.OwinContext.GetUserManager(); ApplicationUser user = await userManager.FindByNameAsync(context.UserName); if (user == null) {…} if (userManager.IsLockedOut(user.Id)) {…} if (!(await userManager.CheckPasswordAsync(user, context.Password))) { … } if (!user.EmailConfirmed) {…} ClaimsIdentity oAuthIdentity = await […]

IQueryable单位或整合测试

我有一个web api,我正在暴露一个端点,如下所示: API /假日?名称= {}名 这是web api的控制器get方法: public IQueryable GetHolidayByName(string name) { return db.Holiday.Where(n => string.Equals(n.Name, name)); } 如何为此检查单元/集成测试以检查名称是否相等? 我可以检查结果是不是null然而有点困惑我怎么能检查名称是否相等: [TestMethod] public void GetHoliday_GetHolidayByName() { // Arrange HolidaysController controller = new HolidaysController(); // Act IQueryable actionResult = controller.GetHolidayByName(“Spain”); //Assert Assert.IsNotNull(actionResult); //any attempt to check names are equal results in a fail //For instance this fails […]

当我需要使用多个参数的POST方法而不是单独的模型?

当我需要在WebApi / MVC应用程序中使用多个参数而不是单独的模型时,您能告诉我吗? 我有一个需要一些参数的动作。 [HttpPost] public InfoViewModel GetInfo(IEnumerable Ids, DocumentType type) { // to do smth } 我也可以将此操作转换为以下内容: [HttpPost] public InfoViewModel GetInfo(RequestViewModel model) { // to do smth } 我需要一个特殊模型用于第二种情况。 public class RequestViewModel { public IEnumerable Ids { get; set; } public DocumentType DocumentType { get; set; } } 我以JSON格式将数据发送到服务器。 你能告诉我这两种方法的优点和缺点吗? 谢谢。

Owin / Katana应该取代Web API吗?

当ASP.NET MVC出现时,微软在许多地方多次宣布它不应该取代ASP.NET Web Forms。 换句话说,它只是您可能觉得有用的另一种技术,或者您可能在其他场景中使用Web窗体。 然而,随着公司进入市场,他们无法拥有技术丛林,因为这太昂贵了。 他们通常选择一种成熟的技术,坚持使用它,在它上面构建并扩展它并在其中重复使用元素以降低成本。 现在我们正试图决定从Web API迁移到Owin / Katana。 我们只是想知道我们100%搬到Owin是否可以? 我问这个问题的原因是因为我们为Web API创建了一个非常丰富的代码库,包括流式传输,压缩,身份validation,UGC规范化,I18N和L10N支持等等。 如果我们想要迁移到Owin,我们需要再次为Owin重新创建这些工具/实用程序,因为它的体系结构与Web API不同。 我们想转移到Owin,因为它是更快,更轻,自托管的服务器,似乎是微软服务技术的未来。 我们完全转移到Owin并想象通过Owin提供所有服务的未来是否安全,我们是否停止使用Web API?

用于generics类型的ASP.NET Web API模型绑定器

与对应的MVC问题一致 ,有没有办法在ASP.NET Web API中为generics类型创建模型绑定器? 如果是,那么如何在绑定器中处理类型检查和实例化? 假设模型绑定器用于URL参数,请考虑一下 [ModelBinder(typeof(MyTypeModelBinder))] public class MyType { //… } 和 public class MyTypeModelBinder : IModelBinder { public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { // check if type if valid? Something like this… if (!(bindingContext.ModelType.IsGenericType && bindingContext.ModelType.GetGenericTypeDefinition() == typeof(MyType))) { return false; } // create instance…using Activator? } }

WebAPI OData $跳过自定义IQueryable double应用

我已经实现了一个通过WebAPI OData端点公开的自定义IQueryable。 控制器的Get()结构相当标准: [EnableQuery( AllowedQueryOptions = AllowedQueryOptions.Count | AllowedQueryOptions.Filter | AllowedQueryOptions.OrderBy | AllowedQueryOptions.Skip | AllowedQueryOptions.Top)] [ODataRoute] public PageResult Get(ODataQueryOptions queryOptions) { var bars = new QueryableData(_provider); var result = ((IQueryable)queryOptions .ApplyTo(bars, new ODataQuerySettings(new ODataQuerySettings { EnableConstantParameterization = false, EnsureStableOrdering = false }))).ToList(); var count = _provider.Count; return new PageResult(result, null, count); } 我看到的奇怪的行为是,在返回PageResult后应用查询字符串中的OData $ Skip。 […]

如何使用swagger swashbuckle保护生成的API文档

我已经使用swagger swashbukle实现了API文档。 现在我想在我的网站上发布生成的文档作为帮助文件。 如何保护此链接并发布?

DryIOC容器配置用于属性注入

我已经搜索了一个简单的例子来说明如何配置DryIoc容器以简单地将依赖关系注入属性,就像它注入构造函数args一样。 鉴于以下工作示例…… 集装箱登记: public static void Register(HttpConfiguration config) { var c = new Container().WithWebApi(config); c.Register(Reuse.Singleton); c.Register(Reuse.Singleton); } 小部件服务: public class WidgetService : IWidgetService { private readonly IWidgetRepository _widgetRepository; public WidgetService(IWidgetRepository widgetRepository) { _widgetRepository = widgetRepository; } public IList GetWidgets() { return _widgetRepository.GetWidgets().ToList(); } } 小部件存储库: public class WidgetRepository : IWidgetRepository { private readonly IList _widgets; […]

从WebAPI返回PageResult而不是格式化为OData

我正在尝试将基于WebAPI的应用程序从WebAPI RC迁移到发布版本。 它接受一些查询参数,并返回ATOM格式的OData。 由于它是一个正在运行的服务,我需要保持当前的行为。 我已经更改了API方法以返回包含我的数据的PageResult 。 根据MSDN上的支持OData查询选项文章应该是我需要做的全部,但它不起作用。 我得到了结果,但它总是格式化为JSON。 我已经尝试将Accept请求标题更改为application/atom+xml ,但它似乎没有任何区别。 我也试过在我的WebApiConfig添加以下行没有明显的效果: configuration.EnableQuerySupport(); configuration.Formatters.InsertRange(0, ODataMediaTypeFormatters.Create()); 我尝试清除现有的格式化程序,只是为了看看会发生什么。 我刚刚收到406 Not Acceptable错误。 所以似乎OData格式化程序可能没有报告他们可以处理请求/响应?

在Web Api控制器中将JSON反序列化为字典

我有这样的JSON字符串: ‘{“1″:[1,3,5],”2″:[2,5,6],”3”:[5,6,8]}’ 我想将其发送到Web Api控制器而不使用ajax请求更改: $.ajax({ type: “POST”, url: “Api/Serialize/Dict”, data: JSON.stringify(sendedData), dataType: “json” }); 在Web Api我有这样的方法: [HttpPost] public object Dict(Dictionary<int, List> sendedData) { //code goes here return null; } 总是sendedData == null. 换句话说:我不知道如何将JSON反序列化为(Dictionary<int, List> 。 谢谢你的答案。