尝试读取Web时抛出错误.config配置部分
本文关键字:错误 config 配置部 出错 读取 Web | 更新日期: 2023-09-27 18:03:11
我想知道是否有可能配置ServiceStack在主机标头中使用API密钥验证调用?
我找到了一个例子:http://rossipedia.com/blog/2013/03/06/simple-api-key-authentication-with-servicestack/
,但由于某些原因,在我的Clients.cs中,它看起来像这样:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
namespace Servicestack_MVC.Models
{
public static class Clients
{
private static Lazy<ClientSection> section = new Lazy<ClientSection>(() =>
(ClientSection)ConfigurationManager.GetSection("apiClients"));
public static bool VerifyKey(string apiKey)
{
return section.Value.Cast<ClientSection.ClientElement>()
.SingleOrDefault(ce => ce.ApiKey == apiKey);
}
}
}
我得到错误:
错误9实例参数:无法从"Servicestack_MVC.Models"转换。ClientSection' to 'System.Linq.IQueryable'和
错误10 'Servicestack_MVC.Models. 'ClientSection'不包含'Cast'的定义,并且最佳扩展方法重载'System.Linq.Queryable.Cast(System.Linq.IQueryable)'有一些无效参数
在网页部分。config我添加了:
<section name="apiClients" type="ClientSection" requirePermission="false"/>
并添加了
部分<apiClients>
<clients>
<client name="Client1" apiKey="somelongrandomkey" />
<client name="Client2" apiKey="somelongrandomkey" />
<!-- etc -->
</clients>
</apiClients>
谁能告诉我我做错了什么?多谢
这实际上是我帖子上的一个错误。已经修好了。实际的代码应该是这样的:
public static bool VerifyKey(string apiKey)
{
return section.Value.Cast<ClientSection.ClientElement>()
.Any(ce => ce.ApiKey == apiKey);
}
同样,您的配置节处理程序需要完全限定。从它的外观来看,似乎您已经将代码放置在Servicestack_MVC.Models
名称空间中。
在这种情况下,您的<section>
标签需要看起来像这样:
<section name="apiClients" type="Servicestack_MVC.Models.ClientSection" requirePermission="false"/>
希望有帮助!