2010-12-15 2 views
3

PowerPivot에서 안전한 WCF 데이터 서비스를 작성하는 데 문제가 있습니다. 이 서비스는 정상적으로 작동하며 문제없이 PowerPivot의 데이터를 사용할 수 있습니다.Excel 용 보안 WCF 데이터 서비스 작성 PowerPivot

내 문제는 PowerPivot (데이터 피드 고급 설정)의 데이터 피드에 대한 사용자 ID와 암호를 입력 할 때 WCF 서비스 내부에서 액세스 할 수없는 것으로 보입니다. 데이터베이스에 대해 인증 할 때 사용자 ID와 암호를 모두 사용하고 싶지만, 먼저 사용자 ID와 암호를 가져와야합니다. :)

PowerPivot 전용 보안 WCF 데이터 서비스를 작성하는 좋은 예가 있습니까?

대단히 감사합니다.

답변

0

나는이 같은 일에 어려움을 겪고 있으며, 일부 연구 후에 나를 압연있어이 블로그 게시물을 발견 : 한마디로

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

, 당신은에 교장을 할 수 있도록해야 할 몇 가지 작업이있다 서비스 호출로 연결됩니다.

+0

정말 감사 : 엑셀의 PowerPivot이 버그를 참조

에 대한

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

중요. – user326502

-1

은의 PowerPivot 클라이언트에 대한 기본 인증을 MSDN에서 전체 다운로드 샘플

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로 새 처리기를 구성하십시오. 답변에 대한 PowerPivot not sending Authorization header in Basic Authentication to OData Svc

+0

이 링크가 질문에 대답 할 수 있지만 답변 [필수] (http://meta.stackoverflow.com/a/8259)의 필수 부분을 포함하고 참조 용 링크를 제공하는 것이 좋습니다. 링크 된 페이지가 변경되면 링크 전용 답변이 유효하지 않게 될 수 있습니다. – bummi

+0

당신 말이 맞아요, 그리고 보통 나는 더 잘할 수 있지만, 나는 침대에서 전화를하고있었습니다. 나는 OP가 요구 한 "좋은 본보기"에 대한 링크가 전혀없는 것보다 낫다고 생각했습니다. –