博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SharePoint 2007 采用表单验证 一段源码
阅读量:6874 次
发布时间:2019-06-26

本文共 6953 字,大约阅读时间需要 23 分钟。

到网上一篇文章 ,做了试试看,第一次失败,最后终于解决了!原来是Sharepoint前台读取那个记录用户信息的文件,总是提示“Access Denied”,因为代码会直接抛Exception,而此时sharepoint因为验证问题,无法显示此错误,导致我也不清楚错误出在什么地方了。最后苦思冥想,在原代码的ValidateUser()中加入的异常的捕获处理,大功告成!不仅发现了问题所在,还看到了登陆不成功的对话框!看来自己写的代码,对异常处理也需要控制啊,尤其是那种为其他系统写组件,用以替换/实现某一功能的!
  1
None.gif
using
 System;
  2
None.gif
using
 System.Collections.Generic;
  3
None.gif
using
 System.Text;
  4
None.gif
using
 System.Web;
  5
None.gif
using
 System.Web.Security;
  6
None.gif
using
 System.IO;
  7
None.gif
using
 System.Collections.Specialized;
  8
None.gif
  9
None.gif
namespace
 MOSSSecurity
 10
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
{
 11InBlock.gif    public class TextFileMembershipProvider : MembershipProvider
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 13InBlock.gif        private String _sFilePath = "";
 14InBlock.gif
 15InBlock.gif        public String FilePath
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 17ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _sFilePath; }
 18ExpandedSubBlockEnd.gif        }
 19InBlock.gif
 20InBlock.gif        private IDictionary<String, String> LoadAllUsers()
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 22InBlock.gif            if (String.IsNullOrEmpty(this.FilePath))
 23ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 24InBlock.gif                throw new InvalidOperationException("FilePath is not set.");
 25ExpandedSubBlockEnd.gif            }
 26InBlock.gif
 27InBlock.gif
 28InBlock.gif            Dictionary<String, String> result = new Dictionary<String, String>();
 29InBlock.gif
 30InBlock.gif            StreamReader reader = new StreamReader(FilePath, Encoding.Default);
 31InBlock.gif            while (true)
 32ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 33InBlock.gif                String sLine = reader.ReadLine();
 34InBlock.gif                if (sLine == null)
 35ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 36InBlock.gif                    break;
 37ExpandedSubBlockEnd.gif                }
 38InBlock.gif                if (sLine.Trim().Length == 0)
 39ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 40InBlock.gif                    continue;
 41ExpandedSubBlockEnd.gif                }
 42InBlock.gif                String[] line = sLine.Split(':');
 43InBlock.gif                result.Add(line[0], line[1]);
 44ExpandedSubBlockEnd.gif            }
 45InBlock.gif
 46InBlock.gif            return result;
 47ExpandedSubBlockEnd.gif        }
 48InBlock.gif
 49InBlock.gif        private void WriteAllUsers(IDictionary<String, String> users)
 50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 51InBlock.gif            if (String.IsNullOrEmpty(this.FilePath))
 52ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 53InBlock.gif                throw new InvalidOperationException("FilePath is not set.");
 54ExpandedSubBlockEnd.gif            }
 55InBlock.gif
 56InBlock.gif            using (StreamWriter writer = new StreamWriter(this.FilePath, false))
 57ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 58InBlock.gif                foreach (String userId in users.Keys)
 59ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 60InBlock.gif                    writer.WriteLine(userId + ":" + users[userId]);
 61ExpandedSubBlockEnd.gif                }
 62ExpandedSubBlockEnd.gif            }
 63ExpandedSubBlockEnd.gif        }
 64InBlock.gif
 65InBlock.gif        public override void Initialize(string name, NameValueCollection config)
 66ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 67InBlock.gif            base.Initialize(name, config);
 68InBlock.gif
 69InBlock.gif            _sFilePath = config["filePath"];
 70ExpandedSubBlockEnd.gif        }
 71InBlock.gif
 72InBlock.gif        public override string ApplicationName
 73ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 74InBlock.gif            get
 75ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 76InBlock.gif                return "/";
 77ExpandedSubBlockEnd.gif            }
 78InBlock.gif            set
 79ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 80InBlock.gif                
 81ExpandedSubBlockEnd.gif            }
 82ExpandedSubBlockEnd.gif        }
 83InBlock.gif
 84InBlock.gif        public override bool ChangePassword(string username, string oldPassword, string newPassword)
 85ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 86InBlock.gif            return true;
 87ExpandedSubBlockEnd.gif        }
 88InBlock.gif
 89InBlock.gif        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
 90ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 91InBlock.gif            return true;
 92ExpandedSubBlockEnd.gif        }
 93InBlock.gif
 94InBlock.gif        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 96InBlock.gif            IDictionary<String, String> users = this.LoadAllUsers();
 97InBlock.gif            if (users.ContainsKey(username))
 98ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 99InBlock.gif                status = MembershipCreateStatus.DuplicateUserName;
100InBlock.gif                return null;
101ExpandedSubBlockEnd.gif            }
102InBlock.gif
103InBlock.gif            users.Add(username, password);
104InBlock.gif            this.WriteAllUsers(users);
105InBlock.gif
106InBlock.gif            status = MembershipCreateStatus.Success;
107InBlock.gif
108InBlock.gif            MembershipUser user = new MembershipUser(this.Name, username, username, email, passwordQuestion, "", isApproved, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
109InBlock.gif            return user;
110ExpandedSubBlockEnd.gif        }
111InBlock.gif
112InBlock.gif        public override bool DeleteUser(string username, bool deleteAllRelatedData)
113ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
114InBlock.gif            IDictionary<String, String> users = this.LoadAllUsers();
115InBlock.gif            if (users.ContainsKey(username))
116ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
117InBlock.gif                users.Remove(username);
118InBlock.gif                this.WriteAllUsers(users);
119InBlock.gif                return true;
120ExpandedSubBlockEnd.gif            }
121InBlock.gif            else
122ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
123InBlock.gif                return false;
124ExpandedSubBlockEnd.gif            }
125ExpandedSubBlockEnd.gif        }
126InBlock.gif
127InBlock.gif        public override bool EnablePasswordReset
128ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
129ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn false; }
130ExpandedSubBlockEnd.gif        }
131InBlock.gif
132InBlock.gif        public override bool EnablePasswordRetrieval
133ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
134ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn false; }
135ExpandedSubBlockEnd.gif        }
136InBlock.gif
137InBlock.gif        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
138ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
139InBlock.gif            totalRecords = 0;
140InBlock.gif            return null;
141ExpandedSubBlockEnd.gif        }
142InBlock.gif
143InBlock.gif        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
144ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
145InBlock.gif            MembershipUserCollection result = new MembershipUserCollection();
146InBlock.gif
147InBlock.gif            IDictionary<String, String> users = this.LoadAllUsers();
148InBlock.gif            foreach (String username in users.Keys)
149ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
150InBlock.gif                if (username.StartsWith(usernameToMatch))
151ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
152InBlock.gif                    result.Add(this.GetUser(usernameToMatch, false));
153ExpandedSubBlockEnd.gif                }
154ExpandedSubBlockEnd.gif            }
155InBlock.gif
156InBlock.gif            totalRecords = users.Count;
157InBlock.gif            return result;
158ExpandedSubBlockEnd.gif        }
159InBlock.gif
160InBlock.gif        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
161ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
162InBlock.gif            MembershipUserCollection result = new MembershipUserCollection();
163InBlock.gif
164InBlock.gif            IDictionary<String, String> users = this.LoadAllUsers();
165InBlock.gif            foreach (String username in users.Keys)
166ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
167InBlock.gif                result.Add(this.GetUser(username, false));
168ExpandedSubBlockEnd.gif            }
169InBlock.gif
170InBlock.gif            totalRecords = users.Count;
171InBlock.gif            return result;
172ExpandedSubBlockEnd.gif        }
173InBlock.gif
174InBlock.gif        public override int GetNumberOfUsersOnline()
175ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
176InBlock.gif            return 0;
177ExpandedSubBlockEnd.gif        }
178InBlock.gif
179InBlock.gif        public override string GetPassword(string username, string answer)
180ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
181InBlock.gif            return "";
182ExpandedSubBlockEnd.gif        }
183InBlock.gif
184InBlock.gif        public override MembershipUser GetUser(string username, bool userIsOnline)
185ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
186InBlock.gif            IDictionary<String, String> users = this.LoadAllUsers();
187InBlock.gif            if (users.ContainsKey(username))
188ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
189InBlock.gif                MembershipUser result = new MembershipUser(this.Name, username, username, """"""truefalse, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
190InBlock.gif                return result;
191ExpandedSubBlockEnd.gif            }
192InBlock.gif            else
193ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
194InBlock.gif                return null;
195ExpandedSubBlockEnd.gif            }
196ExpandedSubBlockEnd.gif        }
197InBlock.gif
198InBlock.gif        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
199ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
200InBlock.gif            return this.GetUser(providerUserKey.ToString(), userIsOnline);
201ExpandedSubBlockEnd.gif        }
202InBlock.gif
203InBlock.gif        public override string GetUserNameByEmail(string email)
204ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
205InBlock.gif            return "";
206ExpandedSubBlockEnd.gif        }
207InBlock.gif
208InBlock.gif        public override int MaxInvalidPasswordAttempts
209ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
210ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn 999; }
211ExpandedSubBlockEnd.gif        }
212InBlock.gif
213InBlock.gif        public override int MinRequiredNonAlphanumericCharacters
214ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
215ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn 0; }
216ExpandedSubBlockEnd.gif        }
217InBlock.gif
218InBlock.gif        public override int MinRequiredPasswordLength
219ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
220ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn 1; }
221ExpandedSubBlockEnd.gif        }
222InBlock.gif
223InBlock.gif        public override int PasswordAttemptWindow
224ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
225ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn 999; }
226ExpandedSubBlockEnd.gif        }
227InBlock.gif
228InBlock.gif        public override MembershipPasswordFormat PasswordFormat
229ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
230ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn MembershipPasswordFormat.Clear; }
231ExpandedSubBlockEnd.gif        }
232InBlock.gif
233InBlock.gif        public override string PasswordStrengthRegularExpression
234ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
235ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ""; }
236ExpandedSubBlockEnd.gif        }
237InBlock.gif
238InBlock.gif        public override bool RequiresQuestionAndAnswer
239ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
240ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn false; }
241ExpandedSubBlockEnd.gif        }
242InBlock.gif
243InBlock.gif        public override bool RequiresUniqueEmail
244ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
245ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn false; }
246ExpandedSubBlockEnd.gif        }
247InBlock.gif
248InBlock.gif        public override string ResetPassword(string username, string answer)
249ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
250InBlock.gif            return "";
251ExpandedSubBlockEnd.gif        }
252InBlock.gif
253InBlock.gif        public override bool UnlockUser(string userName)
254ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
255InBlock.gif            return true;
256ExpandedSubBlockEnd.gif        }
257InBlock.gif
258InBlock.gif        public override void UpdateUser(MembershipUser user)
259ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
260InBlock.gif            
261ExpandedSubBlockEnd.gif        }
262InBlock.gif
263InBlock.gif        public override bool ValidateUser(string username, string password)
264ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
265InBlock.gif            try
266ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
267InBlock.gif                ExceptionMgt.Publish(new Exception(username + "|" + password));
268InBlock.gif
269InBlock.gif                IDictionary<String, String> users = this.LoadAllUsers();
270InBlock.gif                if (!users.ContainsKey(username))
271ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
272InBlock.gif                    return false;
273ExpandedSubBlockEnd.gif                }
274InBlock.gif                if (users[username] != password)
275ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
276InBlock.gif                    return false;
277ExpandedSubBlockEnd.gif                }
278InBlock.gif
279InBlock.gif                return true;
280ExpandedSubBlockEnd.gif            }
281InBlock.gif            catch (Exception ex)
282ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
283InBlock.gif                ExceptionMgt.Publish(ex);
284InBlock.gif                return false;
285ExpandedSubBlockEnd.gif            }
286ExpandedSubBlockEnd.gif        }
287ExpandedSubBlockEnd.gif    }
288ExpandedBlockEnd.gif}
289
None.gif
你可能感兴趣的文章
Windows 下 Hbuilder 真机调试(Android,iphone)
查看>>
async和await
查看>>
称霸Kaggle的十大深度学习技巧
查看>>
只需3步,即可将你的Chromium Edge 浏览器设置成中文
查看>>
【270】IDL处理GeoTIFF数据
查看>>
【071】我的博客园中的插件源码
查看>>
SQL Server 查看空间使用情况的 5 种方法
查看>>
SQL Server dbcc checkdb 做了什么。
查看>>
python--getattr函数
查看>>
element-ui中tabs页设置第一个页面不可关闭
查看>>
Node.js开发入门—HelloWorld再分析
查看>>
准备开源用javascript写Tomcat下的WebApp的项目
查看>>
javascript闭包具体解释
查看>>
CDH- CDH大数据集群运维
查看>>
关于DDD的 认识
查看>>
Python - - 模块 - - 认识模块 和 包
查看>>
jQuery选择器中空格的问题再探究
查看>>
Malab 常用数学函数
查看>>
dom4j详解
查看>>
matlab练习程序(模拟退火SA)
查看>>