跨请求检索和持久化状态
Retrieving and persisting state across requests
我正在编写我的第一个 ASP.NET Web API 应用程序。我熟悉其他 Web 应用程序框架(主要是 Symfony,但也熟悉 Django,以及较小程度的 RoR)。
我有点费劲,想了解从 browser/front 终端客户端向 Web 服务器发送请求后发生的事件序列。
我正在编写一个使用数据库后端的多租户应用程序。我正在使用 ADO 和原始 SQL 访问数据库,我还需要为每个用户存储大量信息,因此基本上,我为用户创建(或从缓存中获取)预加载的上下文。
这里有一些伪代码,说明了我在 ASP.NET 中想要实现的目标。
namespace myApp.Controllers
{
public class FoobarController : ApiController
{
public Response doLogin(request)
{
var ctx = myApplicationContext.getInstance();
var user = ctx.getUser();
if (!user.isLoggedOn())
{
username = request.getParameter('username');
password= request.getParameter('password');
dbManager = ctx.getDbInstance();
resp = dbManager.internalLogin(username, password);
// Load permissions etc for current user, from db
// Store user info in cache ..
}
}
public Response ActionOne(request)
{
ctx = myApplicationContext.getInstance();
user = ctx.getUser();
if (user.hasPermission('xxx'))
{
}
}
}
}
我的问题是,如何实现这种功能:
即:
创建一个应用程序上下文,我可以在其中填充上下文敏感信息,例如数据库连接、邮件程序配置、对象工厂、杂项状态信息等。
访问用户对象(我可以向其添加用户凭据、权限等)
可以访问会话变量等吗?
注释
- 我将在 Linux 上部署 Web 应用程序,我将使用 Apache 作为 Web 服务器。
- 出于此项目的目的,我不想使用任何 Microsoft 技术,例如 Azure、Windows 身份验证等(C# 和 ASP.Net 除外)
- 我想使用原始数据库连接,而不是使用实体管理器(遗留应用程序端口)
I am struggling a bit, to understand the sequence of events that occur after a request is sent from a browser/front end client, to the web server.
为此,我会说 this PDF Poster 给出了 ASP.NET WebAPI 中请求处理的最佳图示。
My question, is, how do I implement this kind of functionality:
Namely:
Create an application context, in which I can populate with context
sensitive information like a database connection, mailer
configuration, object factories, miscellaneous state information etc.
Access a user object (which I can add user credentials, permissions etc to)
Have access to session variables etc?
为此我想说,WebAPI 被设计为无状态的,因此,最好的方法是创建一个持久会话(在数据库中说)并为每个请求使用会话标识符(如会话密钥或令牌)识别用户并获取他的会话变量/上下文信息。
现在,为了实现您在示例中要求的那种功能,可以通过结合使用身份验证过滤器和授权过滤器(More details on implementing them here) 来实现。
WebAPI 中的每个请求首先由处理程序处理,然后在执行请求的操作之前应用过滤器。对于您的示例,身份验证过滤器将包含 DoLogin
函数,user.hasPermission
逻辑将驻留在授权过滤器中,只有操作逻辑将驻留在控制器中的 Action(function) 中。
我正在编写我的第一个 ASP.NET Web API 应用程序。我熟悉其他 Web 应用程序框架(主要是 Symfony,但也熟悉 Django,以及较小程度的 RoR)。
我有点费劲,想了解从 browser/front 终端客户端向 Web 服务器发送请求后发生的事件序列。
我正在编写一个使用数据库后端的多租户应用程序。我正在使用 ADO 和原始 SQL 访问数据库,我还需要为每个用户存储大量信息,因此基本上,我为用户创建(或从缓存中获取)预加载的上下文。
这里有一些伪代码,说明了我在 ASP.NET 中想要实现的目标。
namespace myApp.Controllers
{
public class FoobarController : ApiController
{
public Response doLogin(request)
{
var ctx = myApplicationContext.getInstance();
var user = ctx.getUser();
if (!user.isLoggedOn())
{
username = request.getParameter('username');
password= request.getParameter('password');
dbManager = ctx.getDbInstance();
resp = dbManager.internalLogin(username, password);
// Load permissions etc for current user, from db
// Store user info in cache ..
}
}
public Response ActionOne(request)
{
ctx = myApplicationContext.getInstance();
user = ctx.getUser();
if (user.hasPermission('xxx'))
{
}
}
}
}
我的问题是,如何实现这种功能:
即:
创建一个应用程序上下文,我可以在其中填充上下文敏感信息,例如数据库连接、邮件程序配置、对象工厂、杂项状态信息等。
访问用户对象(我可以向其添加用户凭据、权限等)
可以访问会话变量等吗?
注释
- 我将在 Linux 上部署 Web 应用程序,我将使用 Apache 作为 Web 服务器。
- 出于此项目的目的,我不想使用任何 Microsoft 技术,例如 Azure、Windows 身份验证等(C# 和 ASP.Net 除外)
- 我想使用原始数据库连接,而不是使用实体管理器(遗留应用程序端口)
I am struggling a bit, to understand the sequence of events that occur after a request is sent from a browser/front end client, to the web server.
为此,我会说 this PDF Poster 给出了 ASP.NET WebAPI 中请求处理的最佳图示。
My question, is, how do I implement this kind of functionality:
Namely:
Create an application context, in which I can populate with context sensitive information like a database connection, mailer configuration, object factories, miscellaneous state information etc.
Access a user object (which I can add user credentials, permissions etc to)
Have access to session variables etc?
为此我想说,WebAPI 被设计为无状态的,因此,最好的方法是创建一个持久会话(在数据库中说)并为每个请求使用会话标识符(如会话密钥或令牌)识别用户并获取他的会话变量/上下文信息。
现在,为了实现您在示例中要求的那种功能,可以通过结合使用身份验证过滤器和授权过滤器(More details on implementing them here) 来实现。
WebAPI 中的每个请求首先由处理程序处理,然后在执行请求的操作之前应用过滤器。对于您的示例,身份验证过滤器将包含 DoLogin
函数,user.hasPermission
逻辑将驻留在授权过滤器中,只有操作逻辑将驻留在控制器中的 Action(function) 中。