servicestack,Rest,Authentication,Session State,servicestack" /> servicestack,Rest,Authentication,Session State,servicestack" />

Rest 这是在ServiceStack上执行每次调用的无状态身份验证的正确方法吗?

Rest 这是在ServiceStack上执行每次调用的无状态身份验证的正确方法吗?,rest,authentication,session-state,servicestack,Rest,Authentication,Session State,servicestack,我有REST服务要求,其中有些调用需要身份验证,有些则不需要。绝对不使用状态,因为调用彼此独立。我做了一些似乎有效的东西,但这是不使用会话的正确方式吗 这个问题和我的WCF问题有点关联,我的WCF问题已经回答了 首先我注册了认证方法: Plugins.Add(new AuthFeature(() => new AuthUserSession(), new IAuthProvider[] {

我有REST服务要求,其中有些调用需要身份验证,有些则不需要。绝对不使用状态,因为调用彼此独立。我做了一些似乎有效的东西,但这是不使用会话的正确方式吗

这个问题和我的WCF问题有点关联,我的WCF问题已经回答了

首先我注册了认证方法:

        Plugins.Add(new AuthFeature(() => new AuthUserSession(),
                                    new IAuthProvider[] {
                                        new CustomCredentialsAuthProvider(), //HTML Form post of UserName/Password credentials
                                    }
                        ));
然后,我使用Authenticate属性对各个调用(或服务或DTO)进行属性化:

[Authenticate]
public HelloResponse Post(Hello request)
{
    return new HelloResponse { Result = "Hello, " + request.Name  + " with POST & Auth"};
}
我从执行身份验证的BasicAuthProvider类继承:

public class CustomCredentialsAuthProvider : BasicAuthProvider
{
    public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
    {
        return userName == "dylan" && password == "abc123";
    }

    public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo)
    {
        session.IsAuthenticated = true;

        //Important: You need to save the session!
         authService.SaveSession(session, new TimeSpan(0,0,10)); 
    }
}
公共类CustomCredentialAuthProvider:BasicAuthProvider { 公共覆盖bool TryAuthenticate(IServiceBase authService、字符串用户名、字符串密码) { 返回用户名==“dylan”&&password==“abc123”; } 已验证的公用覆盖无效(IServiceBase authService、IAAuthSession会话、IOAuthTokens令牌、字典authInfo) { session.IsAuthenticated=true; //重要提示:您需要保存会话! SaveSession(session,newtimespan(0,0,10)); } } 如您所见,我确实保存了会话,但它在10秒后超时。这是我确信可以做得更好的部分。不过,它似乎工作得很好

有没有更好的方法来完成我想要完成的事情?
由于这些服务的无会话性质,是否还有任何方法可以删除Auth、AssignRoles和UnassignRoles方法?

如果您想继续使用和支持,您可以添加一个响应过滤器,在执行服务后清除用户会话,例如:

this.ResponseFilters.Add((req, res, dto) => req.RemoveSession());
基本上,在执行每个请求后,它都会清除会话,因此不存在它们已进行身份验证的记录

否则,您可以完全跳过使用ServiceStack的身份验证,只需提供自己的via of(这基本上就是SS Auth在后台所做的)