Приведение IIdentity к IMyIdentity с дополнительными свойствами - PullRequest
0 голосов
/ 23 января 2012

Мне нужно расширить IIdentity (System.Security.Principal.IIdentity) до IMyIdentity с парой дополнительных свойств следующим образом:

public interface IMyIdentity : IIdentity
{
    public bool IsXyz {get;}
    public bool IsAbc {get;}
}

Теперь, когда я выполнял

HttpContext.Current.User.Identity as IIdentity

раньше он работал нормально, но теперь, когда я выполняю

HttpContext.Current.User.Identity as IMyIdentity

, он не работает и приводит к нулю, что является проблемой для меня

Чанк всего метода показан ниже

        public virtual IMyIdentity GetCurrentUserIdentity(bool ignoreXyz)
        {
            if (_userProfile != null && _userProfile.IsAnonymous && (ignoreXyz || _userProfile.PointId > 0))
            {
                return new UserIdentity
                           {
                               Name = _userProfile.UserName,
                               IsAuthenticated = true,
                               AuthenticationType = UserIdentity.AUTHENTICATION_ANONYMOUS
                           };
            }
            else
            {
                return HttpContext.Current.User.Identity as IMyIdentity;
            }
        }

Не могли бы вы дать мне знать, что мне делать, чтобы решить эту проблему?

1 Ответ

0 голосов
/ 23 января 2012

Я наконец-то подумал о переходе свойства из HttpContext.Current.User.Identity к моему типу следующим образом:

public virtual IMyIdentity GetCurrentUserIdentity(bool ignoreXyz)
{
    if (_userProfile != null && _userProfile.IsAnonymous && (ignoreXyz || _userProfile.PointId > 0))
    {
        return new UserIdentity
                   {
                       Name = _userProfile.UserName,
                       IsAuthenticated = true,
                       AuthenticationType = UserIdentity.AUTHENTICATION_ANONYMOUS
                   };
    }
    else
    {
        return new UserIdentity
                   {
                       Name = HttpContext.Current.User.Identity.UserName,
                       IsAuthenticated = HttpContext.Current.User.Identity.IsAuthenticated,
                       AuthenticationType = HttpContext.Current.User.Identity.AuthenticationType
                   };                           
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...