servicestack,C#,Authentication,servicestack" /> servicestack,C#,Authentication,servicestack" />

C# 可以绕过servicestack';非标准身份验证的s身份验证机制

C# 可以绕过servicestack';非标准身份验证的s身份验证机制,c#,authentication,servicestack,C#,Authentication,servicestack,我的身份验证机制与此不同,它无法适应ServiceStack当前的一种身份验证方法(即使覆盖方法“TryAuthenticate”也无法提供解决方案)。那么,是否可以从任意ServiceStack服务进行身份验证 举个例子: 我打开一个普通的HTML登录页面(我使用Angular作为记录) 我登录并调用自定义ServiceStack服务,以便将非标准凭据发送到服务器(当然使用Angular的http指令) 我自己验证凭证。如果正确,我喜欢连接到servicestack身份验证机制,并且可能必须将

我的身份验证机制与此不同,它无法适应ServiceStack当前的一种身份验证方法(即使覆盖方法“TryAuthenticate”也无法提供解决方案)。那么,是否可以从任意ServiceStack服务进行身份验证

举个例子:

  • 我打开一个普通的HTML登录页面(我使用Angular作为记录)
  • 我登录并调用自定义ServiceStack服务,以便将非标准凭据发送到服务器(当然使用Angular的http指令)
  • 我自己验证凭证。如果正确,我喜欢连接到servicestack身份验证机制,并且可能必须将servicestack身份验证cookie发送回浏览器。我说得对吗
    如果有人可以使用3个,我可以调用ServiceStack服务,这些服务具有通过
    [authenticate]
    属性允许的authenticate属性,它需要
    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);        
    }
    
    公共对象任意(CustomAuth请求)
    {
    //验证用户
    var session=base.SessionAs();
    session.IsAuthenticated=true;
    这个.SaveSession(session);
    }
    
    我已更改“返回真值”//用于验证此会话是否通过“到”验证的自定义逻辑返回会话。IsAuthenticated;'。我首先不理解“CustomUserSession”类,但我在以下链接找到了它:。它是从“AuthUserSession”派生的类。我测试了它,它似乎工作!衷心感谢忠诚的ServiceStack用户@Stackbever Yep
    CustomUserSession
    可以是从
    AuthUserSession
    继承的任何类型,由于您希望使用自己的身份验证,我希望您也希望使用带有其他自定义字段的自定义AuthUserSession。在阅读会话文章之前和阅读会话文章时,我从未使用过ServiceStack身份验证。我想知道上述解决方案是否也适用于“记住我”场景(永久会话与临时会话)。所以我试着:1this.SaveSession(会话,新时间跨度(1,0,0,0));',2.验证,3。呼叫安全服务,是的,它工作了!,4.关闭浏览器,5。启动浏览器并再次调用安全服务,但失败。希望它能以某种方式记住会话。@Stackbever如果要记住会话,则需要根据CustomAuth服务中的永久会话id保存会话。您可以将此语句从现有的身份验证服务复制到。非常感谢,刚刚测试了它,它工作正常!完全误解了你的链接。