【问题标题】:Returning custom auth response with ServiceStack使用 ServiceStack 返回自定义身份验证响应
【发布时间】:2013-11-02 07:38:40
【问题描述】:

你好服务堆栈用户 -

下一个问题的 TL;DR 版本是这样的:使用 Servicestack 进行身份验证时,如何返回自定义身份验证响应?

我已经检查了一些关于此主题的先前 Stack 问题/答案,但我还没有想出最好的方法:

How can I extend ServiceStack Authentication Return a custom auth response object from ServiceStack authentication

我想将此对象从 ServiceStack 返回到远程 JsonServiceClient:

public class MyGreatCustomAuthResponse : AuthResponse
{
    public UserModel UserModel { get; set; }
}

我确实有一个客户端当前正在使用默认身份验证响应。我是这样做的:

var AuthResponse = client.Post(new Auth
{
    provider = "credentials",
    UserName = user.user_id, 
    Password = user.password,
    RememberMe = true
});

我假设接受自定义响应的方式可能如下所示:

var AuthResponse = client.Post<MyGreatCustomResponse>(new CustomAuthRequest
{
    provider = "credentials",
   UserName = user.user_id, 
   Password = user.password,
    RememberMe = true
});
//Use the UserModel object in the response for some other function on the client
var UserModel = AuthResponse.UserModel ; 

这很容易吗?如果是这样,如何开始编写代码?

【问题讨论】:

    标签: asp.net-mvc authentication servicestack


    【解决方案1】:

    在 v3 中,除了复制和自定义内置 AuthService 之外,您还可以通过使用 GlobalResponse Filter Attribute 附加自定义 HTTP 标头来添加其他元数据。

    在下一个 v4 版本中,我们在 Authentication and Registration Response DTOs 中添加了 Dictionary&lt;string, string&gt; Meta,因此您可以使用响应过滤器直接在响应 DTO 中添加元数据。

    示例代码

     this.GlobalResponseFilters.Add((req, res, responseDto) =>
            {
                if (res.Dto.GetType() == typeof(AuthenticateResponse))
                {
                    CustomUserSession cs = (CustomUserSession)req.GetSession();
                    Dictionary<string, string> otherData = new Dictionary<string, string>();
                    otherData.Add("CustomData", cs.CustomData.ToString());
                    ((AuthenticateResponse)res.Dto).Meta = otherData;
                }
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      • 2014-11-25
      • 1970-01-01
      • 2023-03-19
      • 2020-02-17
      • 2019-03-12
      相关资源
      最近更新 更多