Il codice C# mostra come collegarsi ad un servizio WCF o WebService ospitato su una WebApplication SharePoint 2013 configurata con la claims authentication passando le credenziali Form Base Authentication (FBA).

C#

//http://www.sgart.it
//chiamo il servizio standard di SharePoint per autenticarmi
//http://sharepoint.lan.local/_vti_bin/authentication.asmx;
using (WsAuth.Authentication auth = new WsAuth.Authentication())
{
  auth.CookieContainer = new CookieContainer();
  //passo le credenziali dell'utente
  WsAuth.LoginResult loginResult = auth.Login("xx_mioutentefba_", "xx_password_xx");

  if (loginResult.ErrorCode == WsAuth.LoginErrorCode.NoError)
  {
    //se non ho errori ricavo il cookie di autenticazione che mi è stato rilasciato
    CookieCollection cc= auth.CookieContainer.GetCookies(new Uri(auth.Url));

    //http://sharepoint.lan.local/sito1/_vti_bin/apps/service.svc
    using (WsApp.Service ws = new WsApp.Service())
    {
      //costruisco il cookie di autenticazione da passare al mio WCF
      ws.CookieContainer = new CookieContainer();
      ws.CookieContainer.Add(cc);

      // invoco i metodi
      var result = ws.GetUserInfo();
    }
  }
}

Segue il WCF usato per l'esempio

File: Service.svc

XML

<%@ ServiceHost Language="C#" Debug="true"
    Service="SgartWCF.Service, $SharePoint.Project.AssemblyFullName$" 
    Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressBasicHttpBindingServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

File: service.svc.cs

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;
using System.ServiceModel.Activation;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Collections;
using Microsoft.SharePoint.Client.Services;

namespace SgartWCF
{
  [BasicHttpBindingServiceMetadataExchangeEndpoint]
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
  public class Service : IService 
  {
    public string GetUserInfo()
    {
      return  SPContext.Current.Web.CurrentUser.LoginName;
    }
  }
}

File: IService .svc

C#

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Collections;

namespace SgartWCF
{
  [ServiceContract]
  public interface IService 
  {
    [OperationContract]
    string GetUserInfo();
  }
}
Tags:
C#236 Claims1 Esempi225 SharePoint498 SharePoint 2010224 SharePoint 2013137 WCF18 Web service16
Potrebbe interessarti anche: