SharePoint 自定义登录页面代码(SharePoint 模拟登陆)
Posted 段传涛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SharePoint 自定义登录页面代码(SharePoint 模拟登陆)相关的知识,希望对你有一定的参考价值。
SharePoint 自定义登录页面代码(SharePoint 模拟登陆)
三年前项目中单点登录模块的,模拟登陆代码,当时由:胡祥文开发。
最近有项目要参考,贴出处理供参考。
namespace HHSDPortal.LAYOUTS.CustomLogin
public partial class Login : Microsoft.SharePoint.IdentityModel.Pages.FormsSignInPage
System.Web.UI.WebControls.Login lo = null;
System.Web.UI.WebControls.TextBox errorControl = null;
protected void Page_Load(object sender, EventArgs e)
// Microsoft.SharePoint.IdentityModel.Pages.FormsSignInPage
protected override void OnLoad(EventArgs e)
try
base.OnLoad(e);
if (!this.IsPostBack)
lo = Page.Master.FindControl("PlaceHolderMain").FindControl("signInControl") as System.Web.UI.WebControls.Login;
errorControl = Page.Master.FindControl("PlaceHolderMain").FindControl("CustomErrorMessage") as System.Web.UI.WebControls.TextBox;
string userName, passWord;
var userInfo = Request.ServerVariables["HTTP_NAMEIDENTIFIER"];
if (userInfo == null)
ShowFormLogin("单点失败,请用用户密码登录!");
return;
//string staffNO = userInfo.Split(',')[1].Substring(3);
string staffNO = GetStaffNo(userInfo);
// var staffNO = this.Request.QueryString["staffno"];
if (!string.IsNullOrEmpty(staffNO))
//获得AD个人属性的自定义字段,如果此字段为空,证明AD和吉大没有同步不允许单点等。后需求改为不允许登陆。
using (DirectoryEntry en = new ADHelper().GetDirectoryEntryByProperty("StaffNO", staffNO.Trim()))
//DirectoryEntry en = null;
if (en == null)
ShowFormLogin("单点失败,请用用户密码登录!");
return;
//从config读取用户名密码,为确保单点所有用户名密码一致。如果允许非证书登录,此处为风险
passWord = System.Configuration.ConfigurationManager.AppSettings["ADUserDefaultPassWord"];
userName = en.Properties["sAMAccountName"][0].ToString();
var success = CustomAuthenticate(userName, passWord);
//登录失败后,显示用户密码登录
if (!success)
ShowFormLogin("单点失败,请用用户密码登录!");
return;
else
//string defaultPageUrl = this.Request.QueryString["Source"];
//if (string.IsNullOrEmpty(defaultPageUrl) || Server.UrlDecode(defaultPageUrl) == "/")
//
// SPSecurity.RunWithElevatedPrivileges(delegate()
//
// using (SPSite site = new SPSite(SPContext.Current.Site.ID))
//
// if (PublishingWeb.IsPublishingWeb(site.RootWeb))
//
// PublishingWeb curPubWeb = PublishingWeb.GetPublishingWeb(site.RootWeb);
// defaultPageUrl = curPubWeb.DefaultPage.ServerRelativeUrl;
// curPubWeb.Close();
//
//
// );
//
base.RedirectToSuccessUrl(defaultPageUrl);
//this.Response.Redirect(defaultPageUrl);
base.RedirectToSuccessUrl();
else
ShowFormLogin("单点失败,请用用户密码登录!");
catch (Exception ex)
ShowFormLogin(ex.Message);
/// <summary>
/// 模拟登陆
/// </summary>
/// <param name="userName"></param>
/// <param name="passWord"></param>
/// <returns></returns>
protected bool CustomAuthenticate(string userName, string passWord)
bool flag = false;
SecurityToken securityToken;
try
securityToken = this.CustomGetSecurityToken(userName, passWord);
SPSessionTokenWriteType sessionTokenWriteType = this.CustomGetSessionTokenWriteType();
this.EstablishSessionWithToken(securityToken, sessionTokenWriteType);
flag = true;
catch (Exception ex)
flag = false;
return flag;
/// <summary>
/// 定义令牌
/// </summary>
/// <param name="userName"></param>
/// <param name="passWord"></param>
/// <returns></returns>
protected virtual SecurityToken CustomGetSecurityToken(string userName, string passWord)
SecurityToken result = null;
SPIisSettings iisSettings = this.IisSettings;
if (!iisSettings.UseClaimsAuthentication || !iisSettings.UseFormsClaimsAuthenticationProvider)
;
Uri appliesTo = this.AppliesTo;
SPFormsAuthenticationProvider formsClaimsAuthenticationProvider = iisSettings.FormsClaimsAuthenticationProvider;
SPFormsAuthenticationOption options = SPFormsAuthenticationOption.None;
try
//参考获得个人信息部分
result = SPSecurityContext.SecurityTokenForFormsAuthentication(appliesTo, formsClaimsAuthenticationProvider.MembershipProvider, formsClaimsAuthenticationProvider.RoleProvider, userName, passWord, options);
catch (Exception ex)
result = null;
return result;
/// <summary>
/// 自定义
/// </summary>
/// <returns></returns>
protected virtual SPSessionTokenWriteType CustomGetSessionTokenWriteType()
SPSessionTokenWriteType result = SPSessionTokenWriteType.WriteDefaultCookie;
if (!SPSecurityTokenServiceManager.Local.UseSessionCookies)
result = SPSessionTokenWriteType.WriteSessionCookie;
return result;
/// <summary>
/// 单点失败
/// </summary>
/// <param name="message"></param>
private void ShowFormLogin(string message)
if (lo != null)
lo.Visible = true;
if (errorControl != null)
errorControl.Text = message;
errorControl.Visible = true;
/// <summary>
/// 获得用户ID
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
private static string GetStaffNo(string content)
string[] arrays = content.Split(new string[] "," , StringSplitOptions.RemoveEmptyEntries);
foreach (string str in arrays)
string[] arrs = str.Split(new string[] "=" , StringSplitOptions.RemoveEmptyEntries);
if (arrs[0].Trim() == "T")
return arrs[1];
return null;
以上是关于SharePoint 自定义登录页面代码(SharePoint 模拟登陆)的主要内容,如果未能解决你的问题,请参考以下文章
将自定义 Web 部件添加到页面 SharePoint 2013 c# csom
SharePoint 2013 在线自定义personimmersive 页面
SharePoint 自定义的列表页面中添加javascript的一个 For循环语句后,该页面就打不开了。