Asp.net mvc 在页面之间安全地传递数据
我希望创建一个新的Web应用程序,允许用户首先输入用户id,然后根据该用户的id在网站上导航。因此,首先用户将搜索id,选择该用户,并根据该id提供选项 目前我正在使用查询字符串来存储ID,所以/Add/2222。 这很好,但我对安全方面不太确定。我考虑过会话和cookie,但我认为它们不适合这种情况。还是加密查询字符串的ID 有人有什么想法吗 谢谢 编辑Asp.net mvc 在页面之间安全地传递数据,asp.net-mvc,security,Asp.net Mvc,Security,我希望创建一个新的Web应用程序,允许用户首先输入用户id,然后根据该用户的id在网站上导航。因此,首先用户将搜索id,选择该用户,并根据该id提供选项 目前我正在使用查询字符串来存储ID,所以/Add/2222。 这很好,但我对安全方面不太确定。我考虑过会话和cookie,但我认为它们不适合这种情况。还是加密查询字符串的ID 有人有什么想法吗 谢谢 编辑 我忘了提到,用户将通过身份验证,并在网站的每个页面上拥有特定权限。数据也存储在数据库中。因此,该站点将查询和编辑/添加到当前数据。因此,基本
我忘了提到,用户将通过身份验证,并在网站的每个页面上拥有特定权限。数据也存储在数据库中。因此,该站点将查询和编辑/添加到当前数据。因此,基本上,这里您似乎担心某些用户可能会修改属于另一个用户的项目。好的,这句话已经告诉我们,您的应用程序中有用户和项目,并且有一些与这些项目相关的角色。你有一个机制来识别那些用户。因此,您可能正在使用某种身份验证,例如内置表单身份验证。现在的问题是:如何确保当前经过身份验证的用户不会修改属于其他用户的产品 好的,您有属于用户的项目。我假设这些信息存储在服务器上的某个地方,可能是数据库或其他什么东西。我建议您使用的一种方法是编写一个自定义授权属性,该属性将检查请求的资源
id
是否实际属于当前经过身份验证的用户
例如:
public class MyAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var isAuthorized = base.AuthorizeCore(httpContext);
if (!isAuthorized)
{
// The user is not authenticated or authorized => no need to continue further
return false;
}
// At this stage we know that the user is authorized => we can fetch
// the username
string username = httpContext.User.Identity.Name;
// Now let's fetch the id of the resource he is trying to manipulate from the request
string id = httpContext.Request["id"];
// All that's left is to verify if the current user is the owner
// of the account
return IsOwnerOfItem(username, id);
}
private bool IsOwnerOfItem(string username, string id)
{
// TODO: query the backend to perform the necessary verifications
// about whether the user has permissions to work with the resource
// pointed by the id parameter
throw new NotImplementedException();
}
}
现在剩下的就是用这个自定义属性装饰您的AddProduct
控制器操作:
[MyAuthorize]
public ActionResult AddProduct(int id)
{
// if we get that far we know that the currently authenticated user
// is the owner of the resource pointed by the id parameter and we
// could proceed respectively
...
}
使用这种方法,您不需要使用任何会话或加密任何内容。加密已经以表单身份验证cookie的形式内置到ASP.NET框架中,该cookie以安全的方式保存当前已验证的用户名。无法操纵此cookie->用户无法模拟为其他用户。因此,一旦您保证了当前用户是谁,剩下的就是执行必要的授权,以确定他是否可以访问请求的资源