2014-03-03 34 views
0

我正在创建一个新的Web应用程序,该应用程序将允许用户首先输入用户ID,然后根据该用户的ID在网站中导航。因此,用户首先搜索ID,选择该用户并根据该ID选择可用选项。在页面之间安全地传递数据

目前我正在使用查询字符串来存储ID,所以.../AddProduct/2222。 这工作正常,但我不太确定安全方面。我曾考虑过会话和cookie,但我认为它们不适合这种情况。或者我加密查询字符串的ID?

有没有人有任何想法?

感谢

编辑

我忘了提,用户将进行身份验证,并有在网站上的每一页上的特定权限。数据也存储在数据库中。因此,该网站将查询和编辑/添加到当前数据。

回答

1

所以,基本上这里你似乎担心某些用户可能会修改属于另一个用户的物品。好吧,这句话告诉我们你已经有了你的应用程序中的用户和项目,并且有一些角色与这些项目相关联。你有一个机制来识别这些用户。因此,您可能正在使用某种身份验证,例如内置的FormsAuthentication。所以现在问题就变成了:如何确保当前经过身份验证的用户不会修改属于其他用户的产品。

好的,所以你有属于用户的物品。我想这些信息存储在服务器的某个地方,大概是数据库或其他东西。我建议你的一种方法是编写一个自定义授权属性,该属性将检查请求的资源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 
    ... 
} 

通过这种方法,你不需要使用任何会话或加密任何东西。加密已经以Forms Authentication cookie的形式嵌入ASP.NET框架中,该cookie以安全的方式保存当前认证的用户名。这个cookie不能被操纵 - >用户不能模仿另一个用户。因此,一旦您得到了关于当前用户的身份的保证,那么剩下的就是执行必要的授权,无论他是否可以访问请求的资源。