2010-12-15 51 views
3

我在寫PowerPivot使用的安全的WCF數據服務時遇到了一些麻煩。該服務工作正常,我可以毫無困難地使用PowerPivot中的數據。爲Excel PowerPivot編寫安全的WCF數據服務

我的問題是,當我在PowerPivot(在數據饋送高級設置中)中輸入數據饋送的用戶ID和密碼時,似乎無法從WCF服務中獲取對它們的訪問權限。我想使用用戶標識和密碼來對數據庫進行身份驗證,但我需要首先查看它們。 :)

是否有任何如何編寫專門用於PowerPivot的安全WCF數據服務的好例子?

非常感謝。

回答

-1

有MSDN上的完整下載的樣本

WCF數據服務與PowerPivot的客戶基本身份驗證

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

更新

好了,現在我已經使用的代碼中的鏈接(我在發佈時正在研究)我知道它的工作原理,所以代碼示例如下:

步驟1:編寫HTTP處理程序以處理所有請求並執行身份驗證(或發出401質詢)。

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> 
    ... 

重要用於Excel的PowerPivot

看到這個錯誤:PowerPivot not sending Authorization header in Basic Authentication to OData Svc

+0

儘管此鏈接可能會回答問題,但最好包含答案的重要部分[此處](http://meta.stackoverflow.com/a/8259)並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – bummi 2014-12-09 23:31:13

+0

你是對的,我通常做得更好,但我在牀上用手機。我認爲,與OP要求的「良好榜樣」的聯繫總比沒有好。 – 2014-12-10 11:33:41