我将 ServiceStack 与 ASP.NET Web 表单和表单身份验证一起使用。每个服务调用我需要以下逻辑:
//-- on 'Begin Request' --
var identity = HttpContext.Current.User.Identity;
if (!identity.IsAuthenticated)
throw new UnauthorizedAccessException();
var user = //lookup user in Db using identity.Name
new UserScope(user) //store this somewhere(?)
//-- on 'End Request' --
var scope = //get user scope
scope.Dispose();
这个逻辑应该去哪里?开始请求部分似乎可以由请求过滤器处理,但我不相信这是最好的地方。应该在哪里UserScope
储存和处置?它必须在同一个线程上创建和处理。
更新
我继承ServiceRunner<TRequest>
并覆盖了OnBeforeExecute
/ OnAfterExecute
。
public override void OnBeforeExecute(IRequestContext requestContext, TRequest request) {
if (!IsLoginRequest()) {
var identity = HttpContext.Current.User.Identity;
if (identity.IsAuthenticated) {
var user = User.GetByUserName(identity.Name);
if (user != null) {
requestContext.SetItem(USER_SCOPE_KEY, new UserScope(user));
return;
}
}
throw new UnauthorizedAccessException();
}
}
public override object OnAfterExecute(IRequestContext requestContext, object response) {
if (!IsLoginRequest()) {
var userScope = (UserScope)requestContext.GetItem(USER_SCOPE_KEY);
if (userScope != null)
userScope.Dispose();
}
return response;
}
并覆盖AppHost.CreateServiceRunner
:
public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(ActionContext actionContext) {
return new MyServiceRunner<TRequest>(this, actionContext);
}
查看源代码ServiceRunner
,似乎OnBeforeExecute
/OnAfterExecute
应该在同一个线程上运行。有什么理由这行不通吗?