转:http://yysyb123.blog.163.com/blog/static/192050472011382421717/

SharePoint2010 自定义代码登录方法 (自定义Form验证的登录界面)(转jlydboy)

困扰了我很久的自定义登录终于解决了,我来共享下我的方法:

代码

        /// <summary>
/// 得到一个安全令牌
/// </summary>
/// <param name="userName">账号</param>
/// <param name="passWord">密码</param>
/// <param name="appliesTo">Uri(例子:base.AppliesTo)</param>
/// <returns>安全令牌</returns>
public static SecurityToken GetSecurityToken(string userName, string passWord, Uri appliesTo)
{
SecurityToken token = null;
if (string.IsNullOrEmpty(userName) ||
string.IsNullOrEmpty(passWord))
return null;
SPWebApplication webApp = SPWebApplication.Lookup(new Uri(SPContext.Current.Web.Url));
SPIisSettings settings = webApp.IisSettings[SPUrlZone.Default];
SPFormsAuthenticationProvider authProvider = settings.FormsClaimsAuthenticationProvider;
token = SPSecurityContext.SecurityTokenForFormsAuthentication(
appliesTo,
authProvider.MembershipProvider,
authProvider.RoleProvider,
userName,
passWord);
return token;
}
/// <summary>
/// 根据安全令牌执行登录
/// </summary>
/// <param name="securityToken">安全令牌</param>
public static void EstablishSessionWithToken(SecurityToken securityToken)
{
if (null == securityToken)
{
throw new ArgumentNullException("securityToken");
}
SPFederationAuthenticationModule fam = SPFederationAuthenticationModule.Current;
if (null == fam)
{
throw new ArgumentException(null, "FederationAuthenticationModule");
}
fam.SetPrincipalAndWriteSessionToken(securityToken);
}
}

如果想要用moss 默认登录的控件来完成登录的话,可以稍微的改动,看代码吧

default.aspx

<%@ Assembly Name="Microsoft.SharePoint.IdentityModel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>
<%@ Page Language="C#" AutoEventWireup="true" Inherits="FormsSignInPage._Default,FormsSignInPage, Version=1.0.0.0, Culture=neutral, PublicKeyToken=72d2bbe72853b8eb" MasterPageFile="~/_layouts/simple.master" %>
<%@ Import Namespace="Microsoft.SharePoint.WebControls" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<asp:Content ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">
<SharePoint:EncodedLiteral runat="server" EncodeMethod="HtmlEncode" Id="ClaimsFormsPageTitle" />
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
<SharePoint:EncodedLiteral runat="server" EncodeMethod="HtmlEncode" Id="ClaimsFormsPageTitleInTitleArea" />
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderSiteName" runat="server"/>
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
<div id="SslWarning" style="color:red;display:none">
<SharePoint:EncodedLiteral runat="server" EncodeMethod="HtmlEncode" Id="ClaimsFormsPageMessage" />
</div>
<script language="javascript" >
//if (document.location.protocol != 'https:') {
var SslWarning = document.getElementById('SslWarning');
SslWarning.style.display = '';
//}
</script>
<asp:login id="loginControl" FailureText="<%$Resources:wss,login_pageFailureText%>" runat="server" width="100%" OnLoggingIn="signInControl_LoggingIn" OnAuthenticate="signInControl_Authenticate">
<layouttemplate>
<asp:label id="FailureText" class="ms-error" runat="server"/>
<table width="100%">
<tr>
<td nowrap="nowrap"><SharePoint:EncodedLiteral runat="server" text="<%$Resources:wss,login_pageUserName%>" EncodeMethod='HtmlEncode'/></td>
<td width="100%"><asp:textbox id="UserName" autocomplete="off" runat="server" class="ms-inputuserfield" width="99%" /></td>
</tr>
<tr>
<td nowrap="nowrap"><SharePoint:EncodedLiteral runat="server" text="<%$Resources:wss,login_pagePassword%>" EncodeMethod='HtmlEncode'/></td>
<td width="100%"><asp:textbox id="password" TextMode="Password" autocomplete="off" runat="server" class="ms-inputuserfield" width="99%"/></td>
</tr>
<tr>
<td nowrap="nowrap"><SharePoint:EncodedLiteral runat="server" text="Secure Code:" EncodeMethod='HtmlEncode'/></td>
<td width="100%">
<asp:textbox id="secureCode" autocomplete="off" runat="server" class="ms-inputuserfield" Width="85%" />
<SharePoint:EncodedLiteral ID="secureCodeLit" runat="server" Text="1234" EncodeMethod="HtmlEncode" />
</td>
</tr>
<tr>
<td colspan="2" align="right"><asp:button id="login" commandname="Login" text="<%$Resources:wss,login_pagetitle%>" runat="server" /></td>
</tr>
<tr>
<td colspan="2"><asp:checkbox id="RememberMe" text="<%$SPHtmlEncodedResources:wss,login_pageRememberMe%>" runat="server" /></td>
</tr>
</table>
</layouttemplate>
</asp:login>
</asp:Content>

default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LoginControl = System.Web.UI.WebControls.Login;
using System.Security;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.IdentityModel;
using Microsoft.SharePoint.IdentityModel.Pages;
using System.IdentityModel.Tokens;
using Microsoft.SharePoint.Administration;
using FormsSignInPage.SSO;
namespace FormsSignInPage
{
public class _Default : IdentityModelSignInPageBase
{
protected LoginControl loginControl;
protected EncodedLiteral ClaimsFormsPageMessage;
protected TextBox secureCode;
protected EncodedLiteral secureCodeLit;
protected void Page_Load(object sender, EventArgs e)
{
try
{
ClaimsFormsPageMessage.Text = "";
loginControl.Focus();
secureCode = (TextBox)loginControl.FindControl("secureCode");
secureCodeLit = (EncodedLiteral)loginControl.FindControl("secureCodeLit");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
/// <summary>
/// 验证登录信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void signInControl_LoggingIn(object sender, LoginCancelEventArgs e)
{
LoginControl login = sender as LoginControl;
login.UserName = login.UserName.Trim();
if (string.IsNullOrEmpty(login.UserName))
{
ClaimsFormsPageMessage.Text = "The server could not sign you in. The user name cannot be empty.";
e.Cancel = true;
}
if (string.IsNullOrEmpty(secureCode.Text) ||
!string.Equals(secureCode.Text.ToLower(), secureCodeLit.Text.ToLower()))
{
ClaimsFormsPageMessage.Text = "The server could not sign you in. Please input correct secure code.";
e.Cancel = true;
}
}
/// <summary>
/// 根据安全令牌执行登录
/// </summary>
/// <param name="securityToken">安全令牌</param>
private void EstablishSessionWithToken(SecurityToken securityToken)
{
if (null == securityToken)
{
throw new ArgumentNullException("securityToken");
}
SPFederationAuthenticationModule fam = SPFederationAuthenticationModule.Current;
if (null == fam)
{
throw new ArgumentException(null, "FederationAuthenticationModule");
}
fam.SetPrincipalAndWriteSessionToken(securityToken);
}
/// <summary>
/// 登录事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void signInControl_Authenticate(object sender, AuthenticateEventArgs e)
{
SecurityToken token = null;
LoginControl formsLoginControl = sender as LoginControl;
if (null != (token = GetSecurityToken(formsLoginControl)))
{
EstablishSessionWithToken(token);
e.Authenticated = true;
base.RedirectToSuccessUrl();
}
}
/// <summary>
/// 获取当前web.congif
/// </summary>
private SPIisSettings IisSettings
{
get
{
SPWebApplication webApp = SPWebApplication.Lookup(new Uri(SPContext.Current.Web.Url));
SPIisSettings settings = webApp.IisSettings[SPUrlZone.Default];
return settings;
}
}
/// <summary>
/// 设置安全令牌
/// </summary>
/// <param name="formsLoginControl">登陆控件</param>
/// <returns> 安全令牌</returns>
private SecurityToken GetSecurityToken(LoginControl formsLoginControl)
{
SecurityToken token = null;
SPIisSettings iisSettings = IisSettings;
Uri appliesTo = base.AppliesTo;
if (string.IsNullOrEmpty(formsLoginControl.UserName) ||
string.IsNullOrEmpty(formsLoginControl.Password))
return null;
SPFormsAuthenticationProvider authProvider = iisSettings.FormsClaimsAuthenticationProvider;
token = SPSecurityContext.SecurityTokenForFormsAuthentication(
appliesTo,
authProvider.MembershipProvider,
authProvider.RoleProvider,
formsLoginControl.UserName,
formsLoginControl.Password);
return token;
}
}
}

SharePoint2010 自定义代码登录方法的更多相关文章

  1. vs2015常用代码块与自定义代码块

    常用代码块 代码段名 描    述 #if 该代码段用#if和#endif命令围绕代码 #region 该代码段用#region和#endregion命令围绕代码 ~ 该代码段插入一个析构函数 att ...

  2. 一种基于自定义代码的asp.net网站首页根据IP自动跳转指定页面的方法!

    一种基于自定义代码的asp.net网站首页根据IP自动跳转指定页面的方法! 对于大中型网站,为了增强用户体验,往往需要根据不同城市站点的用户推送或展现相应个性化的内容,如对于一些大型门户网站的新闻会有 ...

  3. Spring Security入门(2-3)Spring Security 的运行原理 4 - 自定义登录方法和页面

    参考链接,多谢作者: http://blog.csdn.net/lee353086/article/details/52586916 http元素下的form-login元素是用来定义表单登录信息的. ...

  4. webstrom自定义代码块的设置方法

    webstrom里面的自定义代码块叫做活动模版 在文件 -> 设置 -> 编辑器 -> 活动模版可以打开 里面的$var$ 代表一个变量  两个相同的$var$在不全后可以同时修改, ...

  5. ASP.NET Core的身份认证框架IdentityServer4--(5)自定义用户登录(使用官网提供的UI)

    IdentityServer官方提供web页面,可以根据需求修改样式.具体UI下载跟配置参考官网文档. 文档地址:https://identityserver4.readthedocs.io/en/r ...

  6. django自定义实现登录验证-更新版

    django自定义实现登录验证 django内置的登录验证必须让开发者使用django内置的User模块,这会让开发者再某些方面被限制住 下面的模块是我自己自定义实现的django验证,使用方式和dj ...

  7. Spring Security 实战干货:实现自定义退出登录

    文章目录 1. 前言 2. 我们使用 Spring Security 登录后都做了什么 2. 退出登录需要我们做什么 3. Spring Security 中的退出登录 3.1 LogoutFilte ...

  8. Xcode自定义代码块

    到现在才发现原来Xcode有自定义代码块这么神奇的功能,能简化很多无聊的敲重复代码的工作,真是感叹我怎么才知道!!! 具体的设置流程见:http://nshipster.cn/xcode-snippe ...

  9. 如何自定义wordpress登录界面的Logo

    每次登录wp后台都会看到wordpress的logo,会不会有点烦呢?想不想换个新的.自己设定一个呢?那么如何自定义wordpress登录界面的Logo呢? 把代码复制到当前主题的 functions ...

随机推荐

  1. 四、记一次失败的 CAS 搭建 之 结果总是那么伤(客户端)

    ==================================================================================================== ...

  2. 判断webpart类型 How can I tell what type a web part is?

    using(new SPSite("http://mysite/myweb").OpenWeb()){ //give relative path of the webpartpag ...

  3. js高级技巧笔记(一)

    安全的类型检测 Js的类型检测机制并非完全可靠,发生错误否定及错误肯定的情况也不少: 在safari 在对正则表达式应用typeof操作符时返回"function",因此很难确定某 ...

  4. python学习笔记23(时间与日期 (time, datetime包))

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime. time包 time包基于C语言的库函数(library functions).Python的解释器通 ...

  5. 一种高斯模糊渐变动画的实现-b

    关于高斯模糊的方式有很多种,但是如果需要模糊渐变,那么对这种高斯模糊算法的性能要求是比较高的,今天这里重点不讨论算法,只是提供一个动画实现的思路.动画效果如下: 高斯模糊渐变动画 //高斯模糊 -(U ...

  6. 深层解析:构建facebook应用商店推荐引擎

    Under the Hood: Building the App Center recommendation engine   As more apps on Facebook Platform ha ...

  7. Microsoft .NET Framework 4.0安装时发生严重错误 无法安装

    前几天安装Axure,电脑提示没有安装.NET Framework4.0,然后下载安装,又提示如下图所示情况: 在网上找了好多方法,大多都是打开cmd,输入net stop WuAuServ,修改注册 ...

  8. mysql 权限管理

     参考:    http://www.cnblogs.com/Richardzhu/p/3318595.html 一.MySQL权限简介 关于mysql的权限简单的理解就是mysql允许你做你全力以内 ...

  9. 深入js的面向对象学习篇(继承篇)——温故知新(三)

    写这篇有关继承的文章时,突然想起,几天前的面试.因为习惯在学习知识的时候加上自己的理解,很喜欢用自己话来解释,于是乎当面试被问起继承原理时,噼里啪啦一大堆都是自己组织的话,(也可能是因为个人紧张.外加 ...

  10. PE 文件

    一.PE文件基本结构 上图便是PE文件的基本结构.(注意:DOS MZ Header和部分PE header的大小是不变的:DOS stub部分的大小是可变的.) 二.Section 详解 一个PE文 ...