SharePoint 2007 采用表单验证 一段源码

简介:
到网上一篇文章 在SharePoint Server 2007中创建定制的用户管理模块,做了试试看,第一次失败,最后终于解决了!原来是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.gif {
 11InBlock.gif    public class TextFileMembershipProvider : MembershipProvider
 12ExpandedSubBlockStart.gif    {
 13InBlock.gif        private String _sFilePath = "";
 14InBlock.gif
 15InBlock.gif        public String FilePath
 16ExpandedSubBlockStart.gif        {
 17ExpandedSubBlockStart.gif            get return _sFilePath; }
 18ExpandedSubBlockEnd.gif        }

 19InBlock.gif
 20InBlock.gif        private IDictionary<String, String> LoadAllUsers()
 21ExpandedSubBlockStart.gif        {
 22InBlock.gif            if (String.IsNullOrEmpty(this.FilePath))
 23ExpandedSubBlockStart.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.gif            {
 33InBlock.gif                String sLine = reader.ReadLine();
 34InBlock.gif                if (sLine == null)
 35ExpandedSubBlockStart.gif                {
 36InBlock.gif                    break;
 37ExpandedSubBlockEnd.gif                }

 38InBlock.gif                if (sLine.Trim().Length == 0)
 39ExpandedSubBlockStart.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.gif        {
 51InBlock.gif            if (String.IsNullOrEmpty(this.FilePath))
 52ExpandedSubBlockStart.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.gif            {
 58InBlock.gif                foreach (String userId in users.Keys)
 59ExpandedSubBlockStart.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.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.gif        {
 74InBlock.gif            get
 75ExpandedSubBlockStart.gif            {
 76InBlock.gif                return "/";
 77ExpandedSubBlockEnd.gif            }

 78InBlock.gif            set
 79ExpandedSubBlockStart.gif            {
 80InBlock.gif                
 81ExpandedSubBlockEnd.gif            }

 82ExpandedSubBlockEnd.gif        }

 83InBlock.gif
 84InBlock.gif        public override bool ChangePassword(string username, string oldPassword, string newPassword)
 85ExpandedSubBlockStart.gif        {
 86InBlock.gif            return true;
 87ExpandedSubBlockEnd.gif        }

 88InBlock.gif
 89InBlock.gif        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
 90ExpandedSubBlockStart.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.gif        {
 96InBlock.gif            IDictionary<String, String> users = this.LoadAllUsers();
 97InBlock.gif            if (users.ContainsKey(username))
 98ExpandedSubBlockStart.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.gif        {
114InBlock.gif            IDictionary<String, String> users = this.LoadAllUsers();
115InBlock.gif            if (users.ContainsKey(username))
116ExpandedSubBlockStart.gif            {
117InBlock.gif                users.Remove(username);
118InBlock.gif                this.WriteAllUsers(users);
119InBlock.gif                return true;
120ExpandedSubBlockEnd.gif            }

121InBlock.gif            else
122ExpandedSubBlockStart.gif            {
123InBlock.gif                return false;
124ExpandedSubBlockEnd.gif            }

125ExpandedSubBlockEnd.gif        }

126InBlock.gif
127InBlock.gif        public override bool EnablePasswordReset
128ExpandedSubBlockStart.gif        {
129ExpandedSubBlockStart.gif            get return false; }
130ExpandedSubBlockEnd.gif        }

131InBlock.gif
132InBlock.gif        public override bool EnablePasswordRetrieval
133ExpandedSubBlockStart.gif        {
134ExpandedSubBlockStart.gif            get return false; }
135ExpandedSubBlockEnd.gif        }

136InBlock.gif
137InBlock.gif        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
138ExpandedSubBlockStart.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.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.gif            {
150InBlock.gif                if (username.StartsWith(usernameToMatch))
151ExpandedSubBlockStart.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.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.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.gif        {
176InBlock.gif            return 0;
177ExpandedSubBlockEnd.gif        }

178InBlock.gif
179InBlock.gif        public override string GetPassword(string username, string answer)
180ExpandedSubBlockStart.gif        {
181InBlock.gif            return "";
182ExpandedSubBlockEnd.gif        }

183InBlock.gif
184InBlock.gif        public override MembershipUser GetUser(string username, bool userIsOnline)
185ExpandedSubBlockStart.gif        {
186InBlock.gif            IDictionary<String, String> users = this.LoadAllUsers();
187InBlock.gif            if (users.ContainsKey(username))
188ExpandedSubBlockStart.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.gif            {
194InBlock.gif                return null;
195ExpandedSubBlockEnd.gif            }

196ExpandedSubBlockEnd.gif        }

197InBlock.gif
198InBlock.gif        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
199ExpandedSubBlockStart.gif        {
200InBlock.gif            return this.GetUser(providerUserKey.ToString(), userIsOnline);
201ExpandedSubBlockEnd.gif        }

202InBlock.gif
203InBlock.gif        public override string GetUserNameByEmail(string email)
204ExpandedSubBlockStart.gif        {
205InBlock.gif            return "";
206ExpandedSubBlockEnd.gif        }

207InBlock.gif
208InBlock.gif        public override int MaxInvalidPasswordAttempts
209ExpandedSubBlockStart.gif        {
210ExpandedSubBlockStart.gif            get return 999; }
211ExpandedSubBlockEnd.gif        }

212InBlock.gif
213InBlock.gif        public override int MinRequiredNonAlphanumericCharacters
214ExpandedSubBlockStart.gif        {
215ExpandedSubBlockStart.gif            get return 0; }
216ExpandedSubBlockEnd.gif        }

217InBlock.gif
218InBlock.gif        public override int MinRequiredPasswordLength
219ExpandedSubBlockStart.gif        {
220ExpandedSubBlockStart.gif            get return 1; }
221ExpandedSubBlockEnd.gif        }

222InBlock.gif
223InBlock.gif        public override int PasswordAttemptWindow
224ExpandedSubBlockStart.gif        {
225ExpandedSubBlockStart.gif            get return 999; }
226ExpandedSubBlockEnd.gif        }

227InBlock.gif
228InBlock.gif        public override MembershipPasswordFormat PasswordFormat
229ExpandedSubBlockStart.gif        {
230ExpandedSubBlockStart.gif            get return MembershipPasswordFormat.Clear; }
231ExpandedSubBlockEnd.gif        }

232InBlock.gif
233InBlock.gif        public override string PasswordStrengthRegularExpression
234ExpandedSubBlockStart.gif        {
235ExpandedSubBlockStart.gif            get return ""; }
236ExpandedSubBlockEnd.gif        }

237InBlock.gif
238InBlock.gif        public override bool RequiresQuestionAndAnswer
239ExpandedSubBlockStart.gif        {
240ExpandedSubBlockStart.gif            get return false; }
241ExpandedSubBlockEnd.gif        }

242InBlock.gif
243InBlock.gif        public override bool RequiresUniqueEmail
244ExpandedSubBlockStart.gif        {
245ExpandedSubBlockStart.gif            get return false; }
246ExpandedSubBlockEnd.gif        }

247InBlock.gif
248InBlock.gif        public override string ResetPassword(string username, string answer)
249ExpandedSubBlockStart.gif        {
250InBlock.gif            return "";
251ExpandedSubBlockEnd.gif        }

252InBlock.gif
253InBlock.gif        public override bool UnlockUser(string userName)
254ExpandedSubBlockStart.gif        {
255InBlock.gif            return true;
256ExpandedSubBlockEnd.gif        }

257InBlock.gif
258InBlock.gif        public override void UpdateUser(MembershipUser user)
259ExpandedSubBlockStart.gif        {
260InBlock.gif            
261ExpandedSubBlockEnd.gif        }

262InBlock.gif
263InBlock.gif        public override bool ValidateUser(string username, string password)
264ExpandedSubBlockStart.gif        {
265InBlock.gif            try
266ExpandedSubBlockStart.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.gif                {
272InBlock.gif                    return false;
273ExpandedSubBlockEnd.gif                }

274InBlock.gif                if (users[username] != password)
275ExpandedSubBlockStart.gif                {
276InBlock.gif                    return false;
277ExpandedSubBlockEnd.gif                }

278InBlock.gif
279InBlock.gif                return true;
280ExpandedSubBlockEnd.gif            }

281InBlock.gif            catch (Exception ex)
282ExpandedSubBlockStart.gif            {
283InBlock.gif                ExceptionMgt.Publish(ex);
284InBlock.gif                return false;
285ExpandedSubBlockEnd.gif            }

286ExpandedSubBlockEnd.gif        }

287ExpandedSubBlockEnd.gif    }

288ExpandedBlockEnd.gif}

289 None.gif
本文转自Jack Niu博客园博客,原文链接:http://www.cnblogs.com/skywind/archive/2007/04/03/697987.html,如需转载请自行联系原作者
相关文章
|
11月前
|
前端开发 API 数据处理
laravel系列(三) Dcat admin框架工具表单以及普通表单的使用
laravel系列(三) Dcat admin框架工具表单以及普通表单的使用
513 0
Laravel 表单验证 使用技巧
Laravel 表单验证 使用技巧
127 0
|
JavaScript PHP
laravel注册页面(jquery验证)
laravel注册页面(jquery验证)
203 0
|
.NET 开发框架
ueditor编辑器asp版不能上传图片的解决方法 500错误
第一步,先去IIS 中,打开ASP 设置页面,把错误信息发送到浏览器,有助于找到问题。 使用百度开源ueditor编辑器不能上传图片并报服务器500错误 初看是 Uploader.Class 的 fs.CreateFolder( path ) 这条语句不能创建目录,怀疑权限问题,提供所有权限后,依然不行。
2576 0
|
Go
UWP入门(七)--SplitView详解与页面跳转
原文:UWP入门(七)--SplitView详解与页面跳转 官方文档,逼着自己用英文看,UWP开发离不开官方文档 1. SplitView 拆分视图控件 拆分视图控件具有一个可展开/可折叠的窗格和一个内容区域 singleObject ...
1398 0