SharePoint 2010 Form Authentication (SQL) based on existing database

博客地址 http://blog.csdn.net/foxdave

SharePoint 2010 表單認證,基于現有數據庫的用戶信息表

本文主要描述本人配置過程中涉及到的步驟,僅作為參考,不要僅限于此步驟。

另外本文通俗易懂,適合大眾口味兒。

?

I. 開啟并配置基于聲明的身份驗證

打開SharePoint 2010 Management Shell,依次執行以下語句

$app = Get-SPWebApplication "<your webapp url>"
$app.UseClaimsAuthentication = "true"
$app.Update()

進入管理中心->應用程序管理->管理Web應用程序,選中上面的webapp,點擊身份驗證提供程序,點擊默認鏈接彈出驗證配置窗口。

勾選“啟用基于窗體的身份驗證(FBA)”,填入名稱,這里用mp和rp舉例,之后會用到;登錄頁URL這里,可以默認也可以自定義,這里我選擇了自定義,是自己寫的一個登錄頁。

點擊保存完成第一步驟。

?

II. WebConfig配置

需要配置mp和rp的位置有三個,分別是管理中心、webapp端口對應的IIS目錄,以及{SharePoint Root}\WebServices\SecurityToken目錄下的web.config文件

需要添加的內容如下

<membership defaultProvider="i"><providers><!--將以下節點添加到指定位置--><add name="mp" type="<assembly>" />
      </providers></membership>
<roleManager defaultProvider="c" enabled="true" cacheRolesInCookie="false"><providers><!--將以下節點添加到指定位置--><add name="rp" type="<assembly>" /></providers></roleManager>
<connectionStrings><add connectionString="<connStr>" name="Conn" />
  </connectionStrings>

其中assembly為自定義Provider的dll的描述,后面會提到;connStr為數據庫連接串。

?

III. 自定義MembershipProvider

大致的思路是寫兩個sealed類,mp繼承MembershipProvider,rp繼承RoleProvider,我的環境中沒有用到角色,所以rp只做了繼承,注釋掉了

rp代碼

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.Security;namespace Providers
{public sealed class rp : RoleProvider{private bool mWriteExceptionsToEventLog = false;public bool WriteExceptionsToEventLog{get{return mWriteExceptionsToEventLog;}set{mWriteExceptionsToEventLog = value;}}public override void Initialize(string name, NameValueCollection config){base.Initialize(name, config);}private string pApplicationName = "";public override string ApplicationName{get{return pApplicationName;}set{pApplicationName = value;}}public override void AddUsersToRoles(string[] usernames, string[] rolenames){throw new NotImplementedException();}public override void CreateRole(string rolename){throw new NotImplementedException();}public override bool DeleteRole(string rolename, bool throwOnPopulatedRole){throw new NotImplementedException();}public override string[] GetAllRoles(){return null;}public override string[] GetRolesForUser(string username){return null;}public override string[] GetUsersInRole(string rolename){return null;}public override bool IsUserInRole(string username, string rolename){return false;}public override void RemoveUsersFromRoles(string[] usernames, string[] rolenames){throw new NotImplementedException();}public override bool RoleExists(string rolename){return false;}public override string[] FindUsersInRole(string rolename, string usernameToMatch){return null;}private static List<string> GetAllUsers(){return null;}private static List<string> FindAllRoles(){return null;}private List<string> FindRolesForUser(string username){return null;}}
}

mp最少實現以下四個方法,完成在SharePoint上查找添加用戶以及登錄邏輯的自定義處理。

GetAllUsers、GetUser、ValidateUser、FindUsersByName

我這里大致的做法就是用Webconfig中添加的數據庫連接串去操作現有數據庫的用戶表,嘗試用Entities但是好像行不通

mp核心代碼

public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords){MembershipUserCollection spusers = new MembershipUserCollection();List<MIPUser> users = GetAllUsers();foreach (MIPUser user in spusers){MembershipUser spuser = new MembershipUser(this.Name,user.LoginName,user.LoginName,user.LoginName + "@contoso.com","","",true,false,DateTime.Today,DateTime.Today,DateTime.Today,DateTime.Today,DateTime.Today);spusers.Add(spuser);}totalRecords = spusers.Count;return spusers;}
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline){MembershipUser spuser = null;List<MIPUser> users = GetAllUsers();var query = users.Where(u => u.LoginName.IndexOf(providerUserKey.ToString(), StringComparison.CurrentCultureIgnoreCase) >= 0 ||u.DisplayName.IndexOf(providerUserKey.ToString(), StringComparison.CurrentCultureIgnoreCase) >= 0).Select(u => u);if (query.Count() != 0){var user = query.First();spuser = new MembershipUser(this.Name,user.LoginName,user.LoginName,user.LoginName + "@contoso.com","","",true,false,DateTime.Today,DateTime.Today,DateTime.Today,DateTime.Today,DateTime.Today);return spuser;}else{return null;}}
public override MembershipUser GetUser(string username, bool userIsOnline){MembershipUser spuser = null;List<MIPUser> users = GetAllUsers();var query = users.Where(u => u.LoginName.Equals(username, StringComparison.CurrentCultureIgnoreCase) ||u.DisplayName.Equals(username, StringComparison.CurrentCultureIgnoreCase)).Select(u => u);if (query.Count() != 0){var user = query.First();spuser = new MembershipUser(this.Name,user.LoginName,user.LoginName,user.LoginName + "@contoso.com","","",true,false,DateTime.Today,DateTime.Today,DateTime.Today,DateTime.Today,DateTime.Today);return spuser;}else{return null;}}
public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords){MembershipUserCollection spusers = new MembershipUserCollection();List<MIPUser> users = GetAllUsers();var query = users.Where(u => u.LoginName.IndexOf(usernameToMatch, StringComparison.CurrentCultureIgnoreCase) >= 0 ||u.DisplayName.IndexOf(usernameToMatch, StringComparison.CurrentCultureIgnoreCase) >= 0).Select(name => name);foreach (var user in query){MembershipUser spuser = new MembershipUser(this.Name,user.LoginName,user.LoginName,user.LoginName + "@contoso.com","","",true,false,DateTime.Today,DateTime.Today,DateTime.Today,DateTime.Today,DateTime.Today);spusers.Add(spuser);}totalRecords = query.Count();return spusers;}
寫好后將dll添加強命名,部署到GAC。

?

IV. 番外-自定義登錄頁

自定義登錄頁,沒什么難度,直接貼代碼了

ASPX

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="Authentication.login" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title><script language="javascript" for="document" event="onkeydown">if (event.keyCode == 13) {document.getElementById("<%=btnLogin.ClientID %>").click();}</script><script language="javascript" type="text/javascript">function login() {if (document.getElementById("<%=txtUserName.ClientID %>").value == "") {alert('請輸入用戶名');return;}if (document.getElementById("<%=txtPassword.ClientID %>").value == "") {alert('請輸入密碼');return;}document.getElementById("<%=btnLogin.ClientID %>").click();}</script>
</head>
<body><form action="" id="form" runat="server"><table border="0" cellspacing="0" cellpadding="0" class="login_table"><tr><td class="login_td" align="center"><div class="logan_contai"><div class="login_box" style="height: 488px;"><div class="login_top"><div class="login_top_wel">歡迎使用</div><div class="login_top_nav"></div></div><div class="login_main png"><asp:Literal ID="ltError" runat="server"></asp:Literal><div class="login_txt_box"><ul><li><div class="login_name">用戶名</div><div class="login_inpbox"><asp:TextBox ID="txtUserName" runat="server" CssClass="login_input" οnfοcus="this.className='login_input_hov'"MaxLength="100" οnblur="this.className='login_input'"></asp:TextBox><div class="login_wrong"></div></div></li><li><div class="login_name">密 碼</div><div class="login_inpbox"><asp:TextBox ID="txtPassword" runat="server" TextMode="Password" CssClass="login_input"οnfοcus="this.className='login_input_hov'" MaxLength="100" οnblur="this.className='login_input'"></asp:TextBox></div><div class="login_wrong"></div></li><li><div class="login_btn" style="width: 191px; padding: 9px 0 0 155px;"><a href="#" title="" οnclick="login()">登 錄</a></div><div style="display: none;"><asp:Button CssClass="login_btn" Width="191px" ID="btnLogin" runat="server" Text="登錄"OnClick="btnLogin_Click" Style="padding: 9px 0 0 155px" /></div></li></ul></div></div><div class="login_foot png"></div></div></div></td></tr></table></form>
</body>
</html>

CS

using System;
using System.Web.UI;
using Microsoft.SharePoint.IdentityModel;namespace Authentication
{public partial class login : Page{protected void Page_Load(object sender, EventArgs e) { }protected void btnLogin_Click(object sender, EventArgs e){Login(this.txtUserName.Text, this.txtPassword.Text);}private void Login(string username, string passwrod){try{bool status = SPClaimsUtility.AuthenticateFormsUser(Request.Url, username, passwrod);if (!status){ltError.Text = "用戶名或密碼錯誤,請重新輸入!";}else{if (Request.QueryString["ReturnUrl"] != null && Request.QueryString["ReturnUrl"] != ""){Response.Redirect(Request.QueryString["ReturnUrl"]);}else if (Request.QueryString["Source"] != null && Request.QueryString["Source"] != ""){Response.Redirect(Request.QueryString["Source"]);}else{Response.Redirect("~/");}}}catch (Exception ex){ltError.Text = "系統錯誤:<br />";ltError.Text = ex.Message;}}}
}

?

以上就是大致的步驟。PS:在搜索添加SharePoint用戶的時候,無法顯示成名稱,只能顯示登錄名,還不知道如何解決。

參考資料

轉載于:https://www.cnblogs.com/justinliu/p/5961690.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377122.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377122.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377122.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

《MySQL 8.0.22執行器源碼分析(3.1)關于RowIterator》

目錄RowIteratorInit()Read()SetNullRowFlag()UnlockRow()StartPSIBatchMode()EndPSIBatchModeIfStarted()real_iterator()RowIterator 使用選定的訪問方法讀取單個表的上下文&#xff1a;索引讀取&#xff0c;掃描等&#xff0c;緩存的使用等。 它主要是用作接口&#xff0c;但…

hdu 2432法里數列

這題本來完全沒思路的&#xff0c;后來想一想&#xff0c;要不打個表找找規律吧。于是打了個表&#xff0c;真找到規律了。。。 打表的代碼如下&#xff1a; int n; void dfs(int x1, int y1, int x2, int y2) {if (y1 y2 < n) {dfs(x1, y1, x1 x2, y1 y2);printf("…

python學習筆記四——數據類型

1.數字類型&#xff1a; 2.字符串類型&#xff1a; 切片&#xff1a;a[m:n:s] m:起始值 n:結束值&#xff08;不包括n&#xff09; s:步長&#xff0c;負數表示從后向前取值 3.序列&#xff1a;列表&#xff0c;元組和字符串都是序列 序列的兩個主要特點是索引操作符和切片…

小狐貍ChatGPT系統 不同老版本升級至新版數據庫結構同步教程

最新版2.6.7下載&#xff1a;https://download.csdn.net/download/mo3408/88656497 小狐貍GPT付費體驗系統如何升級&#xff0c;該系統更新比較頻繁&#xff0c;也造成了特別有用戶數據情況下升級時麻煩&#xff0c;特別針對會員關心的問題出一篇操作教程&#xff0c;本次教程…

《MySQL 8.0.22執行器源碼分析(3.2)關于HashJoinIterator》

在本文章之前&#xff0c;應該了解的概念&#xff1a; 連接的一些概念、NLJ、BNL、HashJoin算法。 目錄關于join連接probe行保存概念Hashjoin執行流程&#xff08;十分重要&#xff09;HashJoinIterator成員函數講解1、BuildHashTable2、ReadNextHashJoinChunk3、ReadRowFromPr…

json 語法_JSON的基本語法

json 語法JSON which stands for JavaScript Object Notation is a lightweight readable data format that is structurally similar to a JavaScript object much like its name suggests. 代表JavaScript Object Notation的 JSON是一種輕量級的可讀數據格式&#xff0c;其結…

RFC3261(17 事務)

SIP是一個基于事務處理的協議&#xff1a;部件之間的交互是通過一系列相互獨立的消息交換來完成的。特別是&#xff0c;一個SIP 事務由一個單個請求和這個請求的所有應答組成&#xff0c;這些應答包括了零個或者多個臨時應答以及一個或者多個終結應答。在事務中&#xff0c;當請…

HDUOJ---1754 I Hate It (線段樹之單點更新查區間最大值)

I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 33469 Accepted Submission(s): 13168 Problem Description很多學校流行一種比較的習慣。老師們很喜歡詢問&#xff0c;從某某到某某當中&#xff0c;…

WEG的完整形式是什么?

WEG&#xff1a;邪惡邪惡的咧嘴 (WEG: Wicked Evil Grin) WEG is an abbreviation of "Wicked Evil Grin". WEG是“ Wicked Evil Grin”的縮寫 。 It is also known as EWG (Evil Wicked Grin) "Grin" refers to a broad smile. "Wicked" refer…

C# 把數字轉換成鏈表

例如&#xff1a;123456轉換成 1 -> 2 -> 3-> 4-> 5-> 6 View Code static LinkedList<int> CovertIntToLinkedList(int num){Stack<int> stack new Stack<int>();LinkedList<int> result new LinkedList<int>();while (num!0…

《MySQL 8.0.22執行器源碼分析(4.1)Item_sum類以及聚合》

Item_sum類用于SQL聚合函數的特殊表達式基類。 這些表達式是在聚合函數&#xff08;sum、max&#xff09;等幫助下形成的。item_sum類也是window函數的基類。 聚合函數&#xff08;Aggregate Function&#xff09;實現的大部分代碼在item_sum.h和item_sum.cc 聚合函數限制 不…

Java 性能優化實戰記錄(2)---句柄泄漏和監控

前言: Java不存在內存泄漏, 但存在過期引用以及資源泄漏. (個人看法, 請大牛指正) 這邊對文件句柄泄漏的場景進行下模擬, 并對此做下簡單的分析.如下代碼為模擬一個服務進程, 忽略了句柄關閉, 造成不能繼續正常服務的小場景. 1 public class FileHandleLeakExample {2 3 p…

什么是Java文件?

Java文件 (Java files) The file is a class of java.io package. 該文件是java.io包的類。 If we create a file then we need to remember one thing before creating a file. First, we need to check whether a file exists of the same name or not. If a file of the sa…

繞過本地驗證提交HTML數據

我們在入侵一個網站,比如上傳或者自己定義提交的文件時,會在本地的代碼中遇到阻礙,,也就是過 濾,過濾有兩種,一種是在遠程服務器的腳本上進行的過濾,這段代碼是在服務器上運行后產生作用的,這種過 濾方式叫做遠程過濾;另一種是在我們的IE瀏覽器里執行的腳本過濾,就是說是在我們…

《dp補卡——343. 整數拆分、96. 不同的二叉搜索樹》

343. 整數拆分 1、確定dp數組以及下標含義。 dp[i]&#xff1a;分拆數字i&#xff0c;可以得到的最大的乘積 2、確定遞推公式&#xff1a; dp[i]最大乘積出處&#xff1a;從1遍歷j到i&#xff0c;j * dp[i-j] 與 j * (i-j)取最大值。( 拆分j的情況&#xff0c;在遍歷j的過程…

Adroid學習之 從源碼角度分析-禁止使用回退按鈕方案

有時候&#xff0c;不能讓用戶進行回退操作&#xff0c;如何處理&#xff1f; 查看返回鍵觸發了哪些方法。在打開程序后把這個方法禁止了。問題&#xff1a;程序在后臺駐留&#xff0c;這樣就會出現&#xff0c;其他時候也不能使用回退按鈕。如何處理&#xff0c;在onpase()時方…

騎士游歷問題問題_騎士步行問題

騎士游歷問題問題Problem Statement: 問題陳述&#xff1a; There is a chessboard of size NM and starting position (sx, sy) and destination position (dx,dy). You have to find out how many minimum numbers of moves a knight goes to that destination position? 有…

Android基礎之用Eclipse搭建Android開發環境和創建第一個Android項目(Windows平臺)...

一、搭建Android開發環境 準備工作&#xff1a;下載Eclipse、JDK、Android SDK、ADT插件 下載地址&#xff1a;Eclipse:http://www.eclipse.org/downloads/ JDK&#xff1a;http://www.oracle.com/technetwork/java/javase/downloads/jdk7u9-downloads-1859576.html Android SD…

《dp補卡——01背包問題》

目錄01背包[416. 分割等和子集](https://leetcode-cn.com/problems/partition-equal-subset-sum/)[1049. 最后一塊石頭的重量 II](https://leetcode-cn.com/problems/last-stone-weight-ii/)[494. 目標和](https://leetcode-cn.com/problems/target-sum/)01背包 1、dp數組以及…

用JavaScript往DIV動態添加內容

參考&#xff1a;http://zhidao.baidu.com/link?url6jSchyqPiEYCBoKdOmv52YHz9r7MTBms2pK1N6ptOX1kaR2eg320mlW1Sr6n36hpOeOadBxC2rWWGuhZPbms-K <div id"show"></div>要填充的數據為: 這是一個測試例子.jquery&#xff1a;$(function(){ var data …