我将首先解释我要做什么。我有一个正在传入的声明标识符,例如0e.t|saml provider|first.last@domain.local
. 我想把它修剪成first.last@domain.local
.
我很清楚这可以通过简单的字符串格式化来完成,但是,这不是很灵活,所以如果传递了一些我没想到的东西,字符串格式化可能会失败。它更容易出现问题。
我想动态地执行此操作。这是我尝试过的。
尝试EnsureUser
使用上面的声明标识符,然后调用 SPUser.Name:
SPUser spUser = SPContext.Current.Web.EnsureUser(spUserName);
userName = spUser.Name;
我尝试了以下字符串作为 in 的参数EnsureUser
,都导致异常:The user with the specified logonName cannot be found in Active Directory.
0e.t|saml provider|first.last@domain.local
saml provider|first.last@domain.local
first.last@domain.local
同样,所有这些都失败了。
我尝试的另一种方法,使用SPClaimProviderManager
(从这个博客中提取):
SPClaimProviderManager mgr = SPClaimProviderManager.Local;
if (mgr != null && SPClaimProviderManager.IsEncodedClaim(userName))
{
spUserName = mgr.DecodeClaim(SPContext.Current.Web.CurrentUser.LoginName).Value;
}
我想确保我正在尝试解码实际声明,而不是其他内容,所以我调用IsEncodedClaim
. 然而,这总是返回false
。
我的问题:
1)我在这里做错了什么导致这两种尝试都失败了?我需要做些什么不同的事情才能使它们正常运行?
2)有没有更好的方法来获得一个友好的声明用户名而不用字符串解析?