ServiceStack - 角色和权限 [英] ServiceStack - roles and permissions

查看:40
本文介绍了ServiceStack - 角色和权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施 ServiceStack 的角色和权限.我送

I'm implementing ServiceStack's Roles and Permissions. I send

{"UserName":"JASON1","Permissions":["CanAccess"],"Roles":["Admin"]}

通过 http://localhost:15465/api/json/reply/AssignRoles但我收到以下错误:

via http://localhost:15465/api/json/reply/AssignRoles but i got following error:

{
  "AllRoles": [],
  "AllPermissions": [],
  "ResponseStatus": {
    "ErrorCode": "Invalid Role",
    "Message": "Invalid Role",
    "StackTrace": "[AssignRoles: 3/11/2015 11:12:02 PM]:\n[REQUEST: {UserName:JASON1,Permissions:[CanAccess],Roles:[Admin]}]\nServiceStack.HttpError: Invalid Role\r\n   at ServiceStack.RequiredRoleAttribute.AssertRequiredRoles(IRequest req, String[] requiredRoles)\r\n   at ServiceStack.Auth.AssignRolesService.Post(AssignRoles request)\r\n   at lambda_method(Closure , Object , Object )\r\n   at ServiceStack.Host.ServiceRunner`1.Execute(IRequest request, Object instance, TRequest requestDto)",
    "Errors": []
  }
}

解决方案是什么?一些内置角色和权限在哪里?我找不到任何信息?谢谢.

what will be the solutions and where are some built in roles and permissions? i couldn't find any information? Thanks.

推荐答案

AssignRolesService 不应该被任何人调用,默认情况下它只能被 RoleNames.Admin 中的人调用,即 管理员角色.

The AssignRolesService shouldn't be called by anyone, by default it can only be called by someone in the RoleNames.Admin, i.e. Admin Role.

您可以忽略此默认行为,而是使用您自己的自定义服务来分配角色,这只是 IAuthRepository.AssignRoles() API 的包装器,例如:

You can ignore this default behavior by instead using your own custom Service to assign roles which is just a wrapper around the IAuthRepository.AssignRoles() API, e.g:

public class CustomRolesService : Service
{
    public IAuthRepository AuthRepo { get; set; }

    public object Post(AssignRoles request)
    {
        var userAuth = AuthRepo.GetUserAuthByUserName(request.UserName);
        if (userAuth == null)
            throw HttpError.NotFound(request.UserName);

        AuthRepo.AssignRoles(userAuth, request.Roles, request.Permissions);

        return new AssignRolesResponse();
    }
}

Master AuthSecret

为了帮助开发,ServiceStack 还支持指定主密码:

SetConfig(new HostConfig { AdminAuthSecret = "secretz" });

然后您可以使用 QueryString 绕过任何受保护的服务:

Which then lets you by-pass any protected service with the QueryString:

?authsecret=secretz

这篇关于ServiceStack - 角色和权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆