2014-10-24 19 views
5

Đối với SharePoint 2010, chúng tôi đã sử dụng dịch vụ web tùy chỉnh (KHÔNG SOAP!) Để làm cho một số dữ liệu của bên thứ ba khả dụng cho mã JS trong các trang được trình duyệt hiển thị. Đây là dữ liệu nhạy cảm nên chúng tôi đã sử dụng mạo danh để đảm bảo rằng chỉ những người dùng phù hợp mới có thể truy cập vào dữ liệu đó. Giải pháp của chúng tôi không hoạt động thêm trong SharePoint 2013. Vì giải pháp ban đầu khá phức tạp, tôi đã xây dựng một dịch vụ nhỏ và đơn giản trong SP 2013 để điều tra cách thiết lập dịch vụ web có mạo danh. Dịch vụ được triển khai tới một thư mục con của ISAPI.Dịch vụ web tùy chỉnh trong SharePoint 2013 với mạo danh

Đây là cơ sở mà không mạo danh, trong đó hoạt động:

TestService.svc:

<%@ ServiceHost 
    Language="C#" 
    Debug="true" 
    Service="Sandbox.TestService, $SharePoint.Project.AssemblyFullName$" 
    CodeBehind="TestService.svc.cs" 
    Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 

Các mã sau trong TestService.svc.cs là:

using Microsoft.SharePoint.Client.Services; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.ServiceModel.Activation; 
using System.ServiceModel; 
using System.ServiceModel.Web; 

namespace Sandbox 
{ 
    [ServiceContract] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 
    public class TestService 
    { 
     [OperationContract] 
     [WebGet(UriTemplate = "GetAllNumbers", 
      ResponseFormat = WebMessageFormat.Json)] 
     List<int> GetAllNumbers() 
     { 
      List<int> result = new List<int>(); 
      result.AddRange(new[] { 1, 1, 2, 3, 5, 8, 13 }); 
      return result; 
     } 
    } 
} 

Khi tôi thực hiện a GET trên http://pc00175/_vti_bin/Sandbox/TestService.svc/GetAllNumbers Tôi nhận được phản hồi dự kiến ​​[1,1,2,3,5,8,13]. Tốt cho đến nay. Bây giờ tôi cố gắng sử dụng mạo danh:

using Microsoft.SharePoint.Client.Services; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.ServiceModel.Activation; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Security.Principal; 

namespace Sandbox 
{ 
    [ServiceContract] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 
    public class TestService 
    { 
     [OperationContract] 
     [WebGet(UriTemplate = "GetAllNumbers", 
      ResponseFormat = WebMessageFormat.Json)] 
     List<int> GetAllNumbers() 
     { 
      List<int> result = new List<int>(); 
      WindowsImpersonationContext ctx = ServiceSecurityContext.Current.WindowsIdentity.Impersonate(); 
      try 
      { 
       result.AddRange(new[] { 1, 1, 2, 3, 5, 8, 13 }); 
      } 
      finally 
      { 
       ctx.Undo(); 
      } 
      return result; 
     } 
    } 
} 

Bây giờ tôi nhận được một System.InvalidOperationException với thông báo "Danh tính ẩn danh không thể thực hiện mạo danh". khi thực hiện cuộc gọi đến ServiceSecurityContext.Current.WindowsIdentity.Impersonate(). Tôi cần nói với WCF rằng chúng ta cần mạo danh cho cuộc gọi đó. Vì vậy, tôi đã thêm một thuộc tính [OperationBehavior(Impersonation=ImpersonationOption.Required)]:

using Microsoft.SharePoint.Client.Services; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.ServiceModel.Activation; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Security.Principal; 

namespace Sandbox 
{ 
    [ServiceContract] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 
    public class TestService 
    { 
     [OperationContract] 
     [WebGet(UriTemplate = "GetAllNumbers", 
      ResponseFormat = WebMessageFormat.Json)] 
     [OperationBehavior(Impersonation=ImpersonationOption.Required)] 
     List<int> GetAllNumbers() 
     { 
      List<int> result = new List<int>(); 
      WindowsImpersonationContext ctx = ServiceSecurityContext.Current.WindowsIdentity.Impersonate(); 
      try 
      { 
       result.AddRange(new[] { 1, 1, 2, 3, 5, 8, 13 }); 
      } 
      finally 
      { 
       ctx.Undo(); 
      } 
      return result; 
     } 
    } 
} 

Bây giờ tôi tìm ra lỗi sau trong bản ghi SharePoint:

Error when open web service: System.InvalidOperationException: The contract operation 'GetAllNumbers' requires Windows identity for automatic impersonation. A Windows identity that represents the caller is not provided by binding ('WebHttpBinding','http://tempuri.org/') for contract ('TestService','http://tempuri.org/'.  at System.ServiceModel.Dispatcher.SecurityValidationBehavior.WindowsIdentitySupportRule.ValidateWindowsIdentityCapability(Binding binding, ContractDescription contract, OperationDescription operation)  at System.ServiceModel.Dispatcher.SecurityValidationBehavior.WindowsIdentitySupportRule.Validate(ServiceDescription description)  at System.ServiceModel.Dispatcher.SecurityValidationBehavior.System.ServiceModel.Description.IServiceBehavior.Validate(ServiceDescriptio... 

Sau đó, tôi đoán rằng tôi đã có thêm một web.config bên cạnh TestService.svc và thêm chế độ TransportCredentialsOnly nhưng điều đó không giúp được:

<?xml version="1.0"?> 
<configuration> 
    <system.serviceModel> 
    <bindings> 
     <webHttpBinding> 
     <binding> 
      <security mode="TransportCredentialOnly"> 
      <transport clientCredentialType="Ntlm"/> 
      </security> 
     </binding> 
     </webHttpBinding> 
    </bindings> 
    </system.serviceModel> 
</configuration> 

Tôi gặp lỗi tương tự trong tệp nhật ký SharePoint.

Tôi hy vọng ai đó có gợi ý cho tôi.

Cảm ơn bạn đã đọc sách này!

Peter

Trả lời

0

Nếu bạn muốn sử dụng REST, tại sao bạn không chỉ cần tạo ứng dụng cho điều này? Đây là cách dễ dàng hơn để lộ một dịch vụ WCF trong SharePoint. Sau đó, bạn có thể định cấu hình cài đặt bảo mật của riêng mình.

Điều này article sẽ giúp bạn với điều này.

+0

Cảm ơn bạn đã gợi ý. Tôi đã nhầm lẫn mô tả nó như là dịch vụ REST. Nó là một dịch vụ web chung. Chúng tôi không muốn thiết kế lại toàn bộ giải pháp vì nó khá phức tạp và chúng tôi muốn giữ các giải pháp SharePoint 2010 và 2013 càng nhiều càng tốt. –

Các vấn đề liên quan