可以绕过servicestack的非标准身份验证的身份验证机制

时间:2015-01-08 13:27:31

标签: c# authentication servicestack

我的身份验证机制与ServiceStack的当前身份验证方法之间的不同之处不同(即使重写方法'TryAuthenticate'也不提供解决方案)。那么可以从一些任意ServiceStack服务进行身份验证吗?

举个例子:

  1. 我打开一个普通的旧HTML登录页面(我正在使用Angular进行记录)。
  2. 我登录并调用我的自定义ServiceStack服务,以便将非标准凭证发送到服务器(当然使用Angular的http指令)。
  3. 我自己验证凭据。如果正确我喜欢挂钩到servicestack身份验证机制,可能必须将ServiceStack身份验证cookie发送回浏览器。我对么?
  4. 如果有人可以完成3项工作,我可以调用具有authenticate属性的ServiceStack服务

1 个答案:

答案 0 :(得分:1)

要允许通过[Authenticate]属性,需要any one of the registered AuthProviders IsAuthorized()才能返回true,即:

public class CustomAuthProvider : AuthProvider
{
    public CustomAuthProvider()
    {
        this.Provider = "custom";
    }

    public override bool IsAuthorized(
        IAuthSession session, IAuthTokens tokens, Authenticate request=null)
    {
        return true; //custom logic to verify if this session is authenticated
    }

    public override object Authenticate(
        IServiceBase authService, IAuthSession session, Authenticate request)
    {
        throw new NotImplementedException();
    }
}

Plugins.Add(new AuthFeature(() => new CustomUserSession(),
    new IAuthProvider[] {
        new CustomAuthProvider()
    }));

在自定义身份验证服务中,您还应该使用IsAuthenticated=true保存用户会话,例如:

public object Any(CustomAuth request)
{
    //Authenticate User
    var session = base.SessionAs<CustomUserSession>();
    session.IsAuthenticated = true;
    this.SaveSession(session);        
}