zoukankan      html  css  js  c++  java
  • SharePoint2010 自定义代码登录方法

    转: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;
            }
        }
    }
  • 相关阅读:
    PAT1038
    PAT1034
    PAT1033
    PAT1021
    PAT1030
    PAT1026
    PAT1063
    PAT1064
    PAT1053
    PAT1025
  • 原文地址:https://www.cnblogs.com/jackljf/p/3589182.html
Copyright © 2011-2022 走看看