2013-03-09 2 views
1

호스트 헤더의 API 키를 사용하여 호출을 인증하도록 ServiceStack을 구성 할 수 있는지 알아 내려고하고 있습니까? http://rossipedia.com/blog/2013/03/06/simple-api-key-authentication-with-servicestack/Web.config 구성 섹션을 읽으려고 할 때 캐스팅 오류가 발생했습니다.

을하지만, 다음과 같습니다 내 Clients.cs 몇 가지 이유에 대해 :

나는 예를 여기 발견

오류 9 인스턴스 :

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); 
    } 
} 

} 

나는 오류를 얻을 인수 : 'Servicestack_MVC.Models.ClientSection'에서 'System.Linq.IQueryable'로 변환 할 수 없습니다. 및

오류 10 'Servicestack_MVC.Models.ClientSection'에 'Cast'및 최상의 확장 메서드 오버로드에 대한 정의가 없습니다. 'System.Linq.Queryable.Cast (System.Linq.IQueryable)'에 잘못된 인수가 있습니다.

<section name="apiClients" type="ClientSection" requirePermission="false"/> 

및 섹션

을 추가
<apiClients> 
    <clients> 
    <client name="Client1" apiKey="somelongrandomkey" /> 
    <client name="Client2" apiKey="somelongrandomkey" /> 
    <!-- etc --> 
    </clients> 
</apiClients> 

사람이 내가 잘못하시기 바랍니다 뭘 오전 말해 줄 수 : Web.config의 내가 추가 한?

많은 감사합니다.

+0

나는 생각보다 * 조금 더 가깝다. 이제 VerifyKey를 다음과 같이 변경했습니다. return section.Value.Clients.Cast (). SingleOrDefault (ce => ce.ApiKey == apiKey); 'Servicestack_MVC.Models.ClientSection.ClientElement'형식을 암시 적으로 'bool'형식으로 변환 할 수 없습니다. –

+0

죄송합니다! 맞아, 그건 내 게시물에 오타이야. 수정되었습니다. – rossipedia

답변

2

사실 내 게시물에 오류가 있습니다. 그것은 수정되었습니다. 실제 코드는 다음과 같아야합니다.

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"/> 

희망을!

+0

완벽! - 고마워요! 나는 라인 리턴 섹션으로 끝났다 .Value.Clients.Cast (). Any (ce => ce.ApiKey == apiKey); –

관련 문제