[英]Cast Error when trying to read Web.config Config Section
I am trying to find out if it's possible to configure ServiceStack to authenticate a call using an API key in the host header? 我试图找出是否可以配置ServiceStack以使用主机头中的API密钥对呼叫进行身份验证?
I have found an example here: http://rossipedia.com/blog/2013/03/06/simple-api-key-authentication-with-servicestack/ 我在这里找到了一个示例: http : //rossipedia.com/blog/2013/03/06/simple-api-key-authentication-with-servicestack/
but for some reason in my Clients.cs, which looks like this: 但是由于某些原因,我的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);
}
}
}
I get the errors: 我得到了错误:
Error 9 Instance argument: cannot convert from 'Servicestack_MVC.Models.ClientSection' to 'System.Linq.IQueryable' and 错误9实例参数:无法从“ Servicestack_MVC.Models.ClientSection”转换为“ System.Linq.IQueryable”,并且
Error 10 'Servicestack_MVC.Models.ClientSection' does not contain a definition for 'Cast' and the best extension method overload 'System.Linq.Queryable.Cast(System.Linq.IQueryable)' has some invalid arguments 错误10'Servicestack_MVC.Models.ClientSection'不包含'Cast'的定义,并且最佳扩展方法重载'System.Linq.Queryable.Cast(System.Linq.IQueryable)'具有一些无效的参数
In the section of web.config I have added: 在web.config的部分中,我添加了:
<section name="apiClients" type="ClientSection" requirePermission="false"/>
and added the section 并添加了该部分
<apiClients>
<clients>
<client name="Client1" apiKey="somelongrandomkey" />
<client name="Client2" apiKey="somelongrandomkey" />
<!-- etc -->
</clients>
</apiClients>
Can anyone tell me what I am doing wrong please? 谁能告诉我我做错了吗?
Many thanks 非常感谢
That's actually an error on my post. 这实际上是我发布的错误。 It's been fixed.
已修复。 The actual code should look like this:
实际的代码应如下所示:
public static bool VerifyKey(string apiKey)
{
return section.Value.Cast<ClientSection.ClientElement>()
.Any(ce => ce.ApiKey == apiKey);
}
Also, your configuration section handler needs to be fully qualified. 另外,您的配置节处理程序需要完全合格。 From the looks of it, it seems you have placed the code in the
Servicestack_MVC.Models
namespace. 从外观上看,您似乎已将代码放在
Servicestack_MVC.Models
命名空间中。
In that case, your <section>
tag needs to look like this: 在这种情况下,您的
<section>
标记需要如下所示:
<section name="apiClients" type="Servicestack_MVC.Models.ClientSection" requirePermission="false"/>
Hope that helps! 希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.