我有一个项目根据LDAP Active Directory对用户进行身份验证,并根据成员资格限制对某些视图的访问。大部分工作都是在班上完成的/Models/AdAuthenticationService.cs
目前为止一切正常;不过,我似乎无法能够显示在_Layout.cshtmlasp.net mvc如何显示GivenName和Surname而不是名称
我AdAuthenticationService类用户参数,如给定名称和姓有以下:
namespace MyFDM.Models {
public class AdAuthenticationService {
private ClaimsIdentity CreateIdentity(UserPrincipal userPrincipal) {
var identity = new ClaimsIdentity(MyAuthentication.ApplicationCookie, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
identity.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "Active Directory"));
identity.AddClaim(new Claim(ClaimTypes.Name, userPrincipal.SamAccountName));
identity.AddClaim(new Claim(ClaimTypes.GivenName, userPrincipal.GivenName));
identity.AddClaim(new Claim(ClaimTypes.Surname, userPrincipal.Surname));
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userPrincipal.SamAccountName));
if (!String.IsNullOrEmpty(userPrincipal.EmailAddress)) {
identity.AddClaim(new Claim(ClaimTypes.Email, userPrincipal.EmailAddress));
}
而且我_LoginPartial.cshtml包含:
@if (Request.IsAuthenticated)
{
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Hello @User.Identity.Name!<span class="caret"></span></a>
我可以指定任何标识的属性名称;例如:
identity.AddClaim(new Claim(ClaimTypes.Name, userPrincipal.DisplayName));
而这将显示正确的用户名而不是SamAccountName;但我真正需要做的是显示给定名称+姓,如:
@if (Request.IsAuthenticated)
{
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Hello @User.Identity.GivenName + @User.Identity.Surname!<span class="caret"></span></a>
但如果我这样做,我得到以下错误: 错误CS1061“的IIdentity”不包含一个定义对于'GivenName'并且没有扩展方法'GivenName'可以被接受到'IIdentity'类型的第一个参数。