应用于sharepoint URL - 共享身份验证会话

时间:2013-08-07 14:06:17

标签: c# sharepoint authentication forms-authentication

我目前正在使用SharePoint Authentication.asmx在我的C#应用​​程序中创建身份验证会话。这一切都按预期工作,我可以上传文件等

我还在我的应用程序中使用以下链接指向我的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
            {
                ...
            }
        }
    }

1 个答案:

答案 0 :(得分: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.");
        }

    }