2010-12-15 54 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