3

PowerPivotで使用する安全なWCFデータサービスを作成するのに問題があります。サービスは正常に動作し、PowerPivotのデータを問題なく使用できます。

私の問題は、PowerPivot(データフィードの詳細設定)でデータフィードのユーザーIDとパスワードを入力すると、WCFサービス内からそれらにアクセスできないように見えることです。ユーザーIDとパスワードの両方を使用してデータベースに対して認証したいのですが、最初にそれらを取得する必要があります。:)

PowerPivot専用の安全なWCFデータサービスを作成する方法の良い例はありますか?

どうもありがとう。

4

2 に答える 2

0

私はこれと同じことで苦労していました、そしていくつかの研究の後に私を転がらせたこのブログ投稿を見つけました:

http://pfelix.wordpress.com/2011/04/21/wcf-web-api-self-hosting-https-and-http-basic-authentication/

つまり、プリンシパルがサービスコールに流れるようにするために実行する必要のある作業がいくつかあります。

于 2011-06-11T14:04:05.837 に答える
-1

MSDNに完全にダウンロード可能なサンプルがあります

PowerPivotクライアントの基本認証を備えたWCFデータサービス

https://code.msdn.microsoft.com/office/WCF-Data-Service-with-547e9341

アップデート

さて、今私はリンクのコードを使用しました(私が投稿したときに調査していた)それが機能することを知っているので、ここにコード例があります:

ステップ1:すべての要求を処理して認証(または401チャレンジの発行)を実行するHTTPハンドラーを作成します。

namespace WebHostBasicAuth.Modules
{
    public class BasicAuthHttpModule : IHttpModule
    {
        private const string Realm = "My Realm";

        public void Init(HttpApplication context)
        {
            // Register event handlers
            context.AuthenticateRequest += OnApplicationAuthenticateRequest;
            context.EndRequest += OnApplicationEndRequest;
        }

        private static void SetPrincipal(IPrincipal principal)
        {
            Thread.CurrentPrincipal = principal;
            if (HttpContext.Current != null)
            {
                HttpContext.Current.User = principal;
            }
        }

        // TODO: Here is where you would validate the username and password.
        private static bool CheckPassword(string username, string password)
        {
            return username == "user" && password == "password";
        }

        private static void AuthenticateUser(string credentials)
        {
            try
            {
                var encoding = Encoding.GetEncoding("iso-8859-1");
                credentials = encoding.GetString(Convert.FromBase64String(credentials));

                int separator = credentials.IndexOf(':');
                string name = credentials.Substring(0, separator);
                string password = credentials.Substring(separator + 1);

                if (CheckPassword(name, password))
                {
                    var identity = new GenericIdentity(name);
                    SetPrincipal(new GenericPrincipal(identity, null));
                }
                else
                {
                    // Invalid username or password.
                    HttpContext.Current.Response.StatusCode = 401;
                }
            }
            catch (FormatException)
            {
                // Credentials were not formatted correctly.
                HttpContext.Current.Response.StatusCode = 401;
            }
        }

        private static void OnApplicationAuthenticateRequest(object sender, EventArgs e)
        {
            var request = HttpContext.Current.Request;
            var authHeader = request.Headers["Authorization"];
            if (authHeader != null)
            {
                var authHeaderVal = AuthenticationHeaderValue.Parse(authHeader);

                // RFC 2617 sec 1.2, "scheme" name is case-insensitive
                if (authHeaderVal.Scheme.Equals("basic",
                        StringComparison.OrdinalIgnoreCase) &&
                    authHeaderVal.Parameter != null)
                {
                    AuthenticateUser(authHeaderVal.Parameter);
                }
            }
        }

        // If the request was unauthorized, add the WWW-Authenticate header 
        // to the response.
        private static void OnApplicationEndRequest(object sender, EventArgs e)
        {
            var response = HttpContext.Current.Response;
            if (response.StatusCode == 401)
            {
                response.Headers.Add("WWW-Authenticate",
                    string.Format("Basic realm=\"{0}\"", Realm));
            }
        }

        public void Dispose() 
        {
        }
    }
}

手順2:web.configを使用してIISで新しいハンドラーを構成します。

  <system.webServer>
    <modules>
      <add name="BasicAuthHttpModule" 
        type="WebHostBasicAuth.Modules.BasicAuthHttpModule, YourAssemblyName"/>
    </modules>
  ...

ExcelPowerPivotにとって重要

このバグを参照してください:PowerPivotが基本認証のAuthorizationヘッダーをODataSvcに送信しない

于 2014-12-09T22:52:25.903 に答える