1

ホスト ヘッダーの API キーを使用して呼び出しを認証するように ServiceStack を構成できるかどうかを確認しようとしています。

ここで例を見つけました: 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' から 'System.Linq.IQueryable' に変換できません。

エラー 10 'Servicestack_MVC.Models.ClientSection' には 'Cast' の定義が含まれておらず、最適な拡張メソッド オーバーロード 'System.Linq.Queryable.Cast(System.Linq.IQueryable)' に無効な引数が含まれています

web.config のセクションに次を追加しました。

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

セクションを追加しました

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

誰が私が間違っているのか教えてもらえますか?

どうもありがとう

4

1 に答える 1

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

それが役立つことを願っています!

于 2013-03-09T16:48:02.447 に答える