如何在AuthUserSession中存储PreRequestFilter信息
我正在使用ServiceStack构建一个web服务,它必须支持多个供应商。web服务为所有供应商提供基本相同的功能,但也有一些例外 为了尽可能重复使用功能,我提出了以下URL方案:如何在AuthUserSession中存储PreRequestFilter信息,session,authentication,caching,
servicestack,Session,Authentication,Caching,
servicestack,我正在使用ServiceStack构建一个web服务,它必须支持多个供应商。web服务为所有供应商提供基本相同的功能,但也有一些例外 为了尽可能重复使用功能,我提出了以下URL方案: http://localhost/brand1/templates http://localhost/brand2/templates “brand1”和“brand2”不是服务,而是“模板”。模板服务的请求DTO将有一个名为“Brand”的属性,如下所示: 因此,在模板服务中,我知道我要处理的是哪个品牌。这项计
http://localhost/brand1/templates
http://localhost/brand2/templates
“brand1”和“brand2”不是服务,而是“模板”。模板服务的请求DTO将有一个名为“Brand”的属性,如下所示:
因此,在模板服务中,我知道我要处理的是哪个品牌。这项计划运作良好
我想不出的是这个。该服务的用户必须经过身份验证,我无法确定在用户经过身份验证后如何处理该服务的重定向,因为我必须传递品牌信息。我已经创建了自己的CustomAuthProvider类,该类继承了CredentialAuthProvider。在TryAuthenticate方法中,如果我知道authService.GetSession().ReferreUrl属性是什么,我可以将它设置为正确的品牌
到目前为止,我发现获得此信息的唯一方法是注册PreRequestFilter。我的想法是,由于URL(例如)包含品牌,我可以将其存储在我自己的AuthUserSession类中。我不知道该怎么做。我有一个“SessionFactory”方法,可以传递给AuthFeature构造函数。但是我应该在那里做什么呢?如何获得在PreRequestFilter中获得的品牌?将其存储在AppHost的字段中是否安全?我认为这不是因为并发问题。如何将PreRequestFilter绑定到SessionFactory方法
我希望我已经足够清楚地解释了我的问题。按照优先级顺序,您可以在不同的位置设置要重定向到的Url:
Session.refererURL
Url(如果已填充)/auth
发出请求时,使用Continue查询字符串FormData参数(即Authenticate.Continue属性)AuthProvider
的回调URL我想得太多了,因为我没有意识到我在CredentialAuthProvider类的TryAuthenticate方法的IServiceBase参数中有我需要的所有信息 最后,我得出了以下解决方案:
public class CustomCredentialsAuthProvider : CredentialsAuthProvider
{
public override bool TryAuthenticate(IServiceBase authService,
string userName, string password)
{
var session = authService.GetSession();
var origQuery = authService.Request.UrlReferrer.Query;
session.ReferrerUrl = "/error";
var queryString = origQuery.Substring(10); // strip "redirect="
var decodedUrl = HttpUtility.UrlDecode(queryString);
if (!string.IsNullOrWhiteSpace(decodedUrl))
{
var query = new Uri(decodedUrl);
session.ReferrerUrl = query.AbsolutePath;
}
return DoAuthentication(userName, password);
}
}
谢谢你的回答。我已经看到了另一个问题的答案,但恐怕我不知道它如何解决我的问题。我需要知道根据我在请求前过滤器中获得的原始请求URL重定向到哪里。但是我如何将这些信息输入到会话中呢?或者,如何在会话工厂方法中获取原始请求URL?@gerhardwesels这显示了在用户进行身份验证后,可以通过不同的方式更改重定向到的内容。在请求筛选器中,您可以使用
req.GetSession()
访问用户会话,并使用req.SaveSession()
将其保存回ICacheClient。否则,您可以使用req.Info[]
Dictionary来处理希望在同一过程中传递的任何内容。
public class CustomCredentialsAuthProvider : CredentialsAuthProvider
{
public override bool TryAuthenticate(IServiceBase authService,
string userName, string password)
{
var session = authService.GetSession();
var origQuery = authService.Request.UrlReferrer.Query;
session.ReferrerUrl = "/error";
var queryString = origQuery.Substring(10); // strip "redirect="
var decodedUrl = HttpUtility.UrlDecode(queryString);
if (!string.IsNullOrWhiteSpace(decodedUrl))
{
var query = new Uri(decodedUrl);
session.ReferrerUrl = query.AbsolutePath;
}
return DoAuthentication(userName, password);
}
}