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

到网上一篇文章在SharePoint Server 2007中创建定制的用户管理模块,做了试试看,第一次失败,最后终于解决了!原来是Sharepoint前台读取那个记录用户信息的文件,总是提示“Access Denied”,因为代码会直接抛Exception,而此时sharepoint因为验证问题,无法显示此错误,导致我也不清楚错误出在什么地方了。最后苦思冥想,在原代码的ValidateUser()中加入的异常的捕获处理,大功告成!不仅发现了问题所在,还看到了登陆不成功的对话框!看来自己写的代码,对异常处理也需要控制啊,尤其是那种为其他系统写组件,用以替换/实现某一功能的!
  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using System.Web;
  5using System.Web.Security;
  6using System.IO;
  7using System.Collections.Specialized;
  8
  9namespace MOSSSecurity
 10{
 11    public class TextFileMembershipProvider : MembershipProvider
 12    {
 13        private String _sFilePath = "";
 14
 15        public String FilePath
 16        {
 17            get return _sFilePath; }
 18        }

 19
 20        private IDictionary<String, String> LoadAllUsers()
 21        {
 22            if (String.IsNullOrEmpty(this.FilePath))
 23            {
 24                throw new InvalidOperationException("FilePath is not set.");
 25            }

 26
 27
 28            Dictionary<String, String> result = new Dictionary<String, String>();
 29
 30            StreamReader reader = new StreamReader(FilePath, Encoding.Default);
 31            while (true)
 32            {
 33                String sLine = reader.ReadLine();
 34                if (sLine == null)
 35                {
 36                    break;
 37                }

 38                if (sLine.Trim().Length == 0)
 39                {
 40                    continue;
 41                }

 42                String[] line = sLine.Split(':');
 43                result.Add(line[0], line[1]);
 44            }

 45
 46            return result;
 47        }

 48
 49        private void WriteAllUsers(IDictionary<String, String> users)
 50        {
 51            if (String.IsNullOrEmpty(this.FilePath))
 52            {
 53                throw new InvalidOperationException("FilePath is not set.");
 54            }

 55
 56            using (StreamWriter writer = new StreamWriter(this.FilePath, false))
 57            {
 58                foreach (String userId in users.Keys)
 59                {
 60                    writer.WriteLine(userId + ":" + users[userId]);
 61                }

 62            }

 63        }

 64
 65        public override void Initialize(string name, NameValueCollection config)
 66        {
 67            base.Initialize(name, config);
 68
 69            _sFilePath = config["filePath"];
 70        }

 71
 72        public override string ApplicationName
 73        {
 74            get
 75            {
 76                return "/";
 77            }

 78            set
 79            {
 80                
 81            }

 82        }

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

 88
 89        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
 90        {
 91            return true;
 92        }

 93
 94        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
 95        {
 96            IDictionary<String, String> users = this.LoadAllUsers();
 97            if (users.ContainsKey(username))
 98            {
 99                status = MembershipCreateStatus.DuplicateUserName;
100                return null;
101            }

102
103            users.Add(username, password);
104            this.WriteAllUsers(users);
105
106            status = MembershipCreateStatus.Success;
107
108            MembershipUser user = new MembershipUser(this.Name, username, username, email, passwordQuestion, "", isApproved, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
109            return user;
110        }

111
112        public override bool DeleteUser(string username, bool deleteAllRelatedData)
113        {
114            IDictionary<String, String> users = this.LoadAllUsers();
115            if (users.ContainsKey(username))
116            {
117                users.Remove(username);
118                this.WriteAllUsers(users);
119                return true;
120            }

121            else
122            {
123                return false;
124            }

125        }

126
127        public override bool EnablePasswordReset
128        {
129            get return false; }
130        }

131
132        public override bool EnablePasswordRetrieval
133        {
134            get return false; }
135        }

136
137        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
138        {
139            totalRecords = 0;
140            return null;
141        }

142
143        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
144        {
145            MembershipUserCollection result = new MembershipUserCollection();
146
147            IDictionary<String, String> users = this.LoadAllUsers();
148            foreach (String username in users.Keys)
149            {
150                if (username.StartsWith(usernameToMatch))
151                {
152                    result.Add(this.GetUser(usernameToMatch, false));
153                }

154            }

155
156            totalRecords = users.Count;
157            return result;
158        }

159
160        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
161        {
162            MembershipUserCollection result = new MembershipUserCollection();
163
164            IDictionary<String, String> users = this.LoadAllUsers();
165            foreach (String username in users.Keys)
166            {
167                result.Add(this.GetUser(username, false));
168            }

169
170            totalRecords = users.Count;
171            return result;
172        }

173
174        public override int GetNumberOfUsersOnline()
175        {
176            return 0;
177        }

178
179        public override string GetPassword(string username, string answer)
180        {
181            return "";
182        }

183
184        public override MembershipUser GetUser(string username, bool userIsOnline)
185        {
186            IDictionary<String, String> users = this.LoadAllUsers();
187            if (users.ContainsKey(username))
188            {
189                MembershipUser result = new MembershipUser(this.Name, username, username, """"""truefalse, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
190                return result;
191            }

192            else
193            {
194                return null;
195            }

196        }

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

202
203        public override string GetUserNameByEmail(string email)
204        {
205            return "";
206        }

207
208        public override int MaxInvalidPasswordAttempts
209        {
210            get return 999; }
211        }

212
213        public override int MinRequiredNonAlphanumericCharacters
214        {
215            get return 0; }
216        }

217
218        public override int MinRequiredPasswordLength
219        {
220            get return 1; }
221        }

222
223        public override int PasswordAttemptWindow
224        {
225            get return 999; }
226        }

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

232
233        public override string PasswordStrengthRegularExpression
234        {
235            get return ""; }
236        }

237
238        public override bool RequiresQuestionAndAnswer
239        {
240            get return false; }
241        }

242
243        public override bool RequiresUniqueEmail
244        {
245            get return false; }
246        }

247
248        public override string ResetPassword(string username, string answer)
249        {
250            return "";
251        }

252
253        public override bool UnlockUser(string userName)
254        {
255            return true;
256        }

257
258        public override void UpdateUser(MembershipUser user)
259        {
260            
261        }

262
263        public override bool ValidateUser(string username, string password)
264        {
265            try
266            {
267                ExceptionMgt.Publish(new Exception(username + "|" + password));
268
269                IDictionary<String, String> users = this.LoadAllUsers();
270                if (!users.ContainsKey(username))
271                {
272                    return false;
273                }

274                if (users[username] != password)
275                {
276                    return false;
277                }

278
279                return true;
280            }

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

286        }

287    }

288}

289
posted on 2007-04-03 11:29  Jack Niu  阅读(961)  评论(1编辑  收藏  举报

Affiliate Marketing and Web Technology