2

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

我的应用程序中也有指向我的共享点站点的链接,使用:

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
            {
                ...
            }
        }
    }
4

1 回答 1

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.");
        }

    }
于 2013-08-26T16:25:32.163 回答