ServiceStack Web服务安全性

Chr*_*ris 8 security web-services servicestack

嗨,我刚接触Servicestack并下载了他们非常全面的bootstrapapi示例并正在使用它,但我仍然遇到了一些问题.问题在于安全性,发生的事情是我在尝试访问受保护的服务时遇到405错误.使用身份验证服务,似乎我正在进行身份验证.请帮忙解释一下.这是代码:

public class Hello
{
    public string Name { get; set; }
}

public class AuthHello
{
    public string Name { get; set; }
}

public class RoleHello
{
    public string Name { get; set; }
}
public class HelloResponse
{
    public string Result { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

服务:

public class HelloService : ServiceBase<Hello> 
{
    //Get's called by all HTTP Verbs (GET,POST,PUT,DELETE,etc) and endpoints JSON,XMl,JSV,etc
    protected override object Run(Hello request)
    {
        return new HelloResponse { Result = "Hello, Olle är en ÖL ål " + request.Name };
    }
}

[Authenticate()]
public class AuthHelloService : RestServiceBase<AuthHello>
{
    public object Execute(Hello request)
    {
        return new HelloResponse { Result = "Hello, " + request.Name };
    }
}

[RequiredRole("Test")]
public class RoleHelloService : RestServiceBase<RoleHello>
{
    public object Execute(Hello request)
    {
        return new HelloResponse { Result = "Hello, " + request.Name };
    }
}
Run Code Online (Sandbox Code Playgroud)

这是AppHost:

public class HelloAppHost : AppHostBase
    {
        //Tell Service Stack the name of your application and where to find your web services

        public HelloAppHost() : base("Hello Web Services", typeof(HelloService).Assembly) { }

        public override void Configure(Container container)
        {

            //Register all Authentication methods you want to enable for this web app.
        Plugins.Add(new AuthFeature(() => new AuthUserSession(), new IAuthProvider[] {new CustomCredentialsAuthProvider(), //HTML Form post of UserName/Password credentials
            }));
        container.Register<ICacheClient>(new MemoryCacheClient() { FlushOnDispose = false });

            //register user-defined REST-ful urls
            Routes
              .Add<Hello>("/hello")
              .Add<Hello>("/hello/{Name}")
              .Add<AuthHello>("/AuthHello")
              .Add<RoleHello>("/RoleHello");
        }
    }
Run Code Online (Sandbox Code Playgroud)

UPDATE

如果你替换以下所有东西都可以正常工作:RestServiceBase with:ISevice所以现在的问题是为什么.

myt*_*thz 23

首先查看wiki文档

我将首先浏览ServiceStack的Authentication Wiki中的文档,以更好地了解ServiceStack的身份验证的工作原理.维基中有很多文档,所以如果你不确定某些东西,你应该首先参考.这是一个社区维基,如果您认为它可以帮助其他人,请随意扩展它们.

如果行为不明确,请参阅源代码中的实现

如果您不确定某些内容,您应该将RequiredRole源代码作为主要权限引用它的工作方式.RequiredRole只是一个请求过滤器属性,它在具有该属性的每个服务之前运行.

RequiredRole属性只调用您的session.HasRole()方法,如下所示:

public bool HasAllRoles(IAuthSession session)
{
    return this.RequiredRoles
        .All(requiredRole => session != null
            && session.HasRole(requiredRole));
}
Run Code Online (Sandbox Code Playgroud)

因为它只调用您的会话,所以session.HasRole()如果您有自定义会话,则可以覆盖实现.

注册和实现CustomUserSession

Social BootstrapApi项目确实实现了它自己在这里注册CustomSession,但没有覆盖实现,所以它使用基础AuthUserSession.HasRole()中的内置实现,它看起来像Roles集合,看看用户是否具有指定的在他们的会议POCO中的角色:HasRole()

public virtual bool HasRole(string role)
{
    return this.Roles != null && this.Roles.Contains(role);
}
Run Code Online (Sandbox Code Playgroud)

由AuthUserRepository填充的会话属性

角色属性(以及在用户会话大多数其他属性)是由您指定例如,如果您正在使用的AuthUserRepository填充OrmLiteAuthRepositorySocialBootstrapApi在这里所做的不是角色属性的持久化角色列在USERAUTH RDBMS桌子.根据AuthUserRepository,您使用UserAuth/UserOAuthProvider POCO在OrmLite中存储为RDBMS表,或在Redis中存储为文本blob等.

使用AssignRoles/UnAssignRoles服务管理角色和权限

因此,对于具有所需角色(以及传递授权)的用户,应将此角色添加到其UserAuth数据库行条目中.ServiceStack的AuthFeature包括2个用于管理用户权限和角色的服务:

如何最初给某人管理员角色

这些服务确实要求具有管理员角色用户已经过身份验证.您可以通过手动更改特定用户UserAuth.Role列以包含值"Admin" 来执行此操作.相反,Social Bootstrap API项目通过处理CustomUserSessionOnAuthenticated()上的事件来执行此操作,该事件只是检查是否在Web.Config中声明了经过身份验证的用户名,如果是,则调用AssignRoles服务,为该经过身份验证的用户提供管理员角色:

if (AppHost.Config.AdminUserNames.Contains(session.UserAuthName)
    && !session.HasRole(RoleNames.Admin))
{
    var assignRoles = authService.ResolveService<AssignRolesService>();
    assignRoles.Execute(new AssignRoles {
        UserName = session.UserAuthName,
        Roles = { RoleNames.Admin }
    });
}
Run Code Online (Sandbox Code Playgroud)