如何在SharePoint编程登录 [英] How to programatically login on sharepoint
问题描述
我要揭露一个WebMethod,将采取用户名和密码和登录用户。我将如何编程采取这些参数和登录用户的网站,这样的WebMethod完成后,页面重新加载,用户现在是登录。我想避免使用登录ASP的控制,因为我想在一个Ajax调用发送这些参数将WebMethod并将它记录用户的方式。
I want to expose a webmethod that will take in a username and password and login the user. How would I programatically take these parameters and sign in a user to the site, so that after the webmethod is complete, the page reloads and the user is now "logged in". I would like to avoid using the Login asp control because I would like to send these parameters in an ajax call to the webmethod and have it log the user in that way.
推荐答案
我曾经在Windows窗体应用程序这种方法,它工作正常:
I used this method on a Windows Form Application and it works fine:
注意spUsername和spPassword是你有这个code提供的:
Note that spUsername and spPassword are the ones you have to provide in this code:
private bool loginSharePoint()
{
lbLoginStatus.Text = "Logging in Sharepoint server";
bool isValid = false;
//validating Sharepoint login
string spUsername = tbSharePointUsername.Text;
string spPassword = tbSharePointPassword.Text;
pc = new PrincipalContext(ContextType.Domain, spUsername.Split('\\')[0]);
pbLogin.PerformStep();
// validate the credentials
isValid = pc.ValidateCredentials(spUsername.Split('\\')[1], spPassword);
if (isValid)
{
pbLogin.PerformStep();
pbLogin.PerformStep();
site = new SPSite(tbSharePointUrl.Text);
pbLogin.PerformStep();
web = site.OpenWeb();
pbLogin.PerformStep();
if (web.DoesUserHavePermissions(spUsername, SPBasePermissions.Open))
isValid = true;
else
isValid = false;
}
return isValid;
}
这篇关于如何在SharePoint编程登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!