gpt4 book ai didi

c# - 应用到共享点 URL - 共享身份验证 session

转载 作者:太空宇宙 更新时间:2023-11-03 16:09:18 25 4
gpt4 key购买 nike

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

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

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

我遇到的问题是,当我的用户在我的应用程序中单击按钮时,系统会提示我从浏览器登录,这是可以理解的,因为浏览器没有 session 。有什么方法可以与浏览器共享应用程序中的 session 吗?

这是我用来验证的代码:

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

最佳答案

如果您可以维护浏览器对象,以下方法可能会解决您的问题。

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

}

关于c# - 应用到共享点 URL - 共享身份验证 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18105597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com