2013-08-07 108 views
2

我目前使用SharePoint Authentication.asmx從我的C#應用​​程序內創建身份驗證會話。這是所有工作正常,我可以上傳文件等應用程序到共享點URL - 共享身份驗證會話

我使用也有我的應用程序中的鏈接到我的SharePoint網站:

Process.Start("**/documents/Forms/***"); //URL modified for StackOverflow 

我的問題是,系統提示我的用戶從登錄瀏覽器時用我的應用程序點擊一個按鈕,這是可以理解的,因爲瀏覽器沒有會話。有什麼方法可以與我的瀏覽器分享我在應用程序中的會話嗎?

這是我使用驗證碼:

using (SPAuth.Authentication authSvc = new SPAuth.Authentication()) 
    { 
     authSvc.Url = @"***/_vti_bin/Authentication.asmx"; //URL modified for StackOverflow 
     authSvc.CookieContainer = new System.Net.CookieContainer();  

     //set the FBA login information 
     SPAuth.LoginResult result = authSvc.Login(username, password); 

     if (result.ErrorCode == SPAuth.LoginErrorCode.NoError) 
     { 
      try 
      { 
       ... 
      } 
      catch 
      { 
       ... 
      } 
     } 
    } 

回答

0

,如果你能保持下面的方法可以解決你的問題瀏覽器對象。

public void AuthenticateInSharePoint(String url, String login, String password) 
    { 
     try 
     { 
      var uri = new Uri(url); 
      var uriBuilder = new UriBuilder(); 
      uriBuilder.Scheme = uri.Scheme; 
      uriBuilder.Port = uri.Port; 
      uriBuilder.Host = uri.Host; 
      uriBuilder.Path = "_forms/default.aspx"; 
      uriBuilder.Query = String.Format("ReturnUrl={0}", HttpUtility.UrlEncode(uri.LocalPath)); 

      var request = (HttpWebRequest)HttpWebRequest.Create(uriBuilder.ToString()); 
      request.ContentType = "application/x-www-form-urlencoded"; 
      request.AllowAutoRedirect = true; 
      var response = (HttpWebResponse)request.GetResponse(); 

      using (var reader = new StreamReader(response.GetResponseStream())) 
      { 
       var html = reader.ReadToEnd(); 

       var doc = new HtmlDocument(); 
       doc.LoadHtml(html); 

       foreach (var node in doc.DocumentNode.Descendants("script").ToList()) 
        node.Remove(); 

       foreach (var node in doc.DocumentNode.Descendants("link").ToList()) 
        node.Remove(); 

       var form = doc.DocumentNode.Descendants("form").FirstOrDefault(); 
       if (form != null) 
       { 
        form.Attributes["action"].Value = uriBuilder.ToString(); 

        var script = doc.CreateElement("script"); 
        script.InnerHtml = String.Format(@" 
         var input = document.createElement('input'); 
         input.setAttribute('type', 'hidden'); 
         input.setAttribute('name', 'ctl00$PlaceHolderMain$signInControl$login'); 
         input.value = 'Sign In'; 
         document.forms[0].appendChild(input); 

         document.getElementById('ctl00_PlaceHolderMain_signInControl_UserName').value=""{0}""; 
         document.getElementById('ctl00_PlaceHolderMain_signInControl_password').value=""{1}""; 
         document.forms[0].submit();", login, password); 

        var body = doc.DocumentNode.Descendants("body").FirstOrDefault(); 
        if (body != null) 
         body.AppendChild(script); 
       } 

       var builder = new StringBuilder(); 
       using (var writer = new StringWriter(builder)) 
        doc.Save(writer); 

       return Content(builder.ToString(), response.ContentType); 
      } 
     } 
     catch (Exception e) 
     { 
      Log.Error(e, "Failed to authenticate user in SharePoint."); 
     } 

    }