标签:
最近做了一个简单的sharepoint webpart开发,功能很简单,就实现FBA密码修改。sharepoint 是默认是没有密码修改功能的,可能是因为账号来源可以任意配置的原因。
如何创建webpart,请参照http://www.cnblogs.com/jianyus/p/4221243.html的博文。
下面是关键代码
1.引用
1 using Microsoft.SharePoint; 2 using System.Web.Security; 3 using System.Web; 4 using Microsoft.SharePoint.Utilities; 5 using Microsoft.SharePoint.Administration;
2.实现
1 protected void Button1_Click(object sender, EventArgs e) 2 { 3 4 ResetPassword(); 5 } 6 7 private void ResetPassword() 8 { 9 SPWeb web = SPContext.Current.Web; 10 11 SPSecurity.RunWithElevatedPrivileges(delegate() 12 { 13 try 14 { 15 16 String ProviderName = GetMembershipProvider(Context);//取得认证提供者名称 17 //web.CurrentUser.Name为当前用户名,textbox1为旧密码,Textbox3为新密码 18 bool re = Membership.Providers[ProviderName].ChangePassword( web.CurrentUser.Name,TextBox1.Text.Trim(),TextBox3.Text.Trim()); 21 if (re) 22 { 23 lable1.Text = "success";//更改成功 24 } 25 else 26 { 27 lable1.Text = "false"; 28 } 29 } 30 31 catch (SPException spex) 32 { 33 lable1.Text = spex.Message; 34 } 35 catch (Exception ex) 36 { 37 lable1.Text = ex.Message; 38 } 39 40 }); 56 57 } 58 59 public static string GetMembershipProvider(HttpContext context) 60 { 61 using (SPSite site = new SPSite(SPUtility.GetPageUrlPath(context))) 62 { 63 return GetMembershipProvider(site); 64 } 65 } 66 67 public static string GetMembershipProvider(SPSite site) 68 { 69 // get membership provider of whichever zone in the web app is fba enabled 70 SPIisSettings settings = GetFBAIisSettings(site); 71 if (settings == null) return null; 72 return settings.FormsClaimsAuthenticationProvider.MembershipProvider; 73 } 74 private static SPIisSettings GetFBAIisSettings(SPSite site) 75 { 76 SPIisSettings settings = null; 77 78 // try and get FBA IIS settings from current site zone 79 try 80 { 81 settings = site.WebApplication.IisSettings[site.Zone]; 82 if (settings.UseFormsClaimsAuthenticationProvider) 83 return settings; 84 } 85 catch 86 { 87 // expecting errors here so do nothing 88 } 89 90 // check each zone type for an FBA enabled IIS site 91 foreach (SPUrlZone zone in Enum.GetValues(typeof(SPUrlZone))) 92 { 93 try 94 { 95 settings = site.WebApplication.IisSettings[(SPUrlZone)zone]; 96 if (settings.UseFormsClaimsAuthenticationProvider) 97 return settings; 98 } 99 catch 100 { 101 // expecting errors here so do nothing 102 } 103 } 104 105 // return null if FBA not enabled 106 return null; 107 }
以上代码有参考:
https://sharepoint2010fba.codeplex.com/
https://fbachangepassword.codeplex.com/
总结:
其中最核心的是:Membership.Providers[ProviderName].ChangePassword( web.CurrentUser.Name,TextBox1.Text.Trim(),TextBox3.Text.Trim());
Membership.Providers[ProviderName] 还有ResetPassword函数,不过是自动随机密码,参考的例子就是用这个方法。
标签:
原文地址:http://www.cnblogs.com/shappy/p/4606522.html