我似乎无法让我们的测试通过ServiceStack服务上的RequiredPermission属性。有人可以帮我弄清楚我在哪里出问题了吗?

假定RequiredPermission使用session.Permissions列表。

我们的UserViewModel设置如下

public class UserViewModel : ViewModelBase
{
    public UserViewModel()
    {
        Groups = new List<GroupModel>();
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; } // This should never be populated on the way out.

    public IList<GroupModel> Groups { get; set; }
}


GroupModel与其中类似的SecuritySettings列表相似。每次对服务层的调用都会返回一个完全混合的UserViewModel,其中包含一个Groups列表,每个Groups都包含一个SecuritySettings列表。

当用户验证身份后,我们运行

    public override void OnAuthenticated ( IServiceBase authService,
                                          IAuthSession session,
                                          IOAuthTokens tokens,
                                          Dictionary<string, string> authInfo )
    {
        session.Id = _userViewModel.Id.ToString();
        session.UserName = _userViewModel.Email;
        session.FirstName = _userViewModel.FirstName;
        session.DisplayName = string.Format( "{0} {1}", _userViewModel.FirstName, _userViewModel.LastName );

        session.Roles = new List<string>();
        session.Permissions = new List<string>();

        if ( _userViewModel.Groups != null )
        {
            foreach ( var group in _userViewModel.Groups )
            {
                // Add user Groups to "Roles"
                session.Roles.Add( group.Name );

                if ( @group.SecuritySettings == null ) continue;
                foreach ( var securitySetting in @group.SecuritySettings )
                {
                    // Add group SecuritySettings to "Permissions"
                    session.Permissions.Add( securitySetting.Name );
                }
            }
        }

        var mapper = new AutoMapper<UserModel>();
        _container.Register( mapper.BuildFrom( _userViewModel ) );

        //Important: You need to save the session!
        authService.SaveSession( session, SessionExpiry );
    }


我遇到的问题是我的测试仍在我的UserServiceInterface方法上返回“未经授权”

    [RequiredPermission("Read User")]
    public object Get( UserRequest request )
    {
        return new UserResponse { User = _userService.GetById( request.Id ) };
    }


我可以确认UserViewModel.Groups[0].SecuritySettings[0].Name == "Read User"

最佳答案

解决此问题的方法是在CustomCredentialsAuthProvider.OnAuthenticated方法末尾调用base.OnAuthenticated方法。

    public override void OnAuthenticated ( IServiceBase authService,
                                           IAuthSession session,
                                           IOAuthTokens tokens,
                                           Dictionary<string, string> authInfo )
    {

        // truncated for brevity

        //Important: You need to save the session!s
        authService.SaveSession( session, SessionExpiry );

        // THIS ENSURES THE SESSION IS ACCESSABLE BY THE APP
        base.OnAuthenticated(authService, session, tokens, authInfo);
    }

关于c# - ServiceStack RequiredPermission无法验证我的用户,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19278599/

10-11 05:59