转: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. 备份了一个nginx的虚拟主机配置文件报错

    [root@localhost vhost]# service nginx restart 停止 nginx:[确定] 正在启动 nginx:nginx: [warn] conflicting ser ...

  2. python logging 日志轮转文件不删除问题

    前言 最近在维护项目的python项目代码,项目使用了 python 的日志模块 logging, 设定了保存的日志数目, 不过没有生效,还要通过contab定时清理数据. 分析 项目使用了 logg ...

  3. 请求管道与IHttpModule接口

    IHttpModule向实现类提供模块初始化和处置事件.  IHttpModule包含兩個方法: public void Init(HttpApplication context);public vo ...

  4. sql之事务和并发

    1.Transaction(事务)是什么: 事务是作为单一工作单元而执行的一系列操作.包括增删查改. 2.事务的种类: 事务分为显示事务和隐式事务: 隐式事务:就是平常我们使用每一条sql 语句就是一 ...

  5. 1060: [ZJOI2007]时态同步 - BZOJ

    Description小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3….进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的 ...

  6. localStorage 便签功能实现

    之前利用localStorage写过手机便签应用,因为蛋疼的换了台三星的屌丝级手机,木 有了测试的工具,没能继续优化维护下去.而在网页上实现便签功能目前来说似乎没有太大 的意义,因为不论是 Firef ...

  7. 原创新闻 11 个最佳 jQuery 滚动条插件

    通过jQuery滚动条插件,你可以换掉千篇一律的默认浏览器滚动条,让你的网站或web项目更具特色,更有吸引力.本文收集了11款非常漂亮.实用的jQuery滚动条插件,你可以轻松将它们应用在自己的网站中 ...

  8. jQuery的优点——(一)

    稍微学点js然后就用起jQuery,用久了,就好像自己都成高手了,想得少了,都觉得自己就是js高手了.哗啦啦的惭愧啊=.= 面试GY的时候,因为在简历上写着大大的会用jQuery快速开发原型页面,然后 ...

  9. Notepad++ 右键菜单自定义配置

    问:想在右键菜单里面多加几个功能,怎么加,比如区块注释 答:其实notepad++的配置文件存放路径不在自己的软件路径,而存在于 xp:C:\Documents and Settings\Admini ...

  10. uva 567

    Floyd 算法   就输入麻烦点 #include <iostream> #include <cstring> #include <cstdlib> #inclu ...