1

我正在为 SharePoint 2010 开发一个小项目,该项目将允许用户使用他们的 OpenID 进行身份验证(以后可能还会更多)。

它目前的工作方式是我有一个基于 SQL 的成员资格和角色提供程序。我的登录页面使用 DotNetOpenAuth 向 OpenID 端点发送身份验证请求。如果我得到肯定的响应,我会从 OpenID 角色读取电子邮件地址,并为具有该电子邮件地址的 MembershipUser 创建一个会话(如果我知道他来自的端点)。如果该用户不存在,我将创建一个新的 MembershipUser 并将端点存储在评论属性中(这样您就不能简单地编辑自己的角色以作为另一个人的电子邮件地址登录)。

这可行,但问题在于创建会话。我目前使用以下代码执行此操作:

//authenticate
SecurityToken token = SPSecurityContext.SecurityTokenForFormsAuthentication(new Uri(SPContext.Current.Web.Url),userProvider,roleProvider,username,user.GetPassword());         
SPFederationAuthenticationModule.Current.SetPrincipalAndWriteSessionToken(token);

但是,只有在会员提供者不加密密码时,该位才有效,因为一旦加密,我就不能只读取用户的密码并在该方法中使用它。

我希望找到一种让用户登录的方法,但我似乎找不到一种简单的方法来做到这一点。

使用

FormsAuthentication.SetAuthCookie(username, false);

只是创建一个异常(我认为这是因为该方法适用于标准 FBA,而不是基于声明的 FBA,但我只是在猜测。带有用户名和密码的内置 FBA 登录控件确实有效。)

System.ArgumentException: Exception of type 'System.ArgumentException' was thrown.  Parameter name: encodedValue   
 at Microsoft.SharePoint.Administration.Claims.SPClaimEncodingManager.DecodeClaimFromFormsSuffix(String encodedValue)    
 at Microsoft.SharePoint.Administration.Claims.SPClaimProviderManager.GetProviderUserKey(String encodedSuffix)    
 at Microsoft.SharePoint.SPGlobal.CreateSPRequestAndSetIdentity(SPSite site, String name, Boolean bNotGlobalAdminCode, String strUrl, Boolean bNotAddToContext, Byte[] UserToken, String userName, Boolean bIgnoreTokenTimeout, Boolean bAsAnonymous)    
 at Microsoft.SharePoint.SPWeb.InitializeSPRequest()    
 at Microsoft.SharePoint.WebControls.SPControl.EnsureSPWebRequest(SPWeb web)    
 at Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext context)    
 at Microsoft.SharePoint.ApplicationRuntime.BaseApplication.Application_PreRequestHandlerExecute(Object sender, EventArgs e)    
 at Microsoft.SharePoint.ApplicationRuntime.SPRequestModule.PreRequestExecuteAppHandler(Object oSender, EventArgs ea)    
 at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()    
 at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

长话短说,我想要一种无需使用密码即可为我的会员创建令牌的方法。

有没有人有这方面的经验,或者知道我如何才能得到我想要的东西?

提前致谢。

4

1 回答 1

1

如果其他人遇到同样的情况,我会说我是如何“解决”这个问题的。

在专业环境中,您很可能会创建自己的会员提供程序/安全令牌服务,这样您就可以在代码中请求令牌,而无需用户密码。

就我而言(由于时间限制)我做不到,所以我采取了更简单的方法。我没有使用加密密码(通过 IIS),而是使用了散列密码(在 web.config 中手动设置)。我还确保允许为我的会员提供商重置密码。然后我使用以下代码发出令牌:

MembershipUser user = membershipProvider.GetUser(username, true);
if(user == null)
{
    //create new user here
}
string password = user.ResetPassword();
SecurityToken token = SPSecurityContext.SecurityTokenForFormsAuthentication(new Uri(SPContext.Current.Web.Url), membershipProviderName, roleProviderName, user.UserName, password, false);
SPFederationAuthenticationModule.Current.SetPrincipalAndWriteSessionToken(token);            

实际上,这意味着表单用户每次登录时都会重置他的密码,但由于他从未真正使用过他的密码,所以问题不大。它可以很好地在 SharePoint 中快速显示 OpenID 支持。

于 2012-05-10T07:10:50.263 回答