Web应用程序中的安全方法

jan*_*ann 6 c# asp.net security web-applications membership-provider

我正在ASP.NET/C#中设计一个Web应用程序,其中每个注册用户都可以根据用户ID添加/修改/删除行.

举个例子:

我将在/route.aspx?routeid=854属于我的页面上编辑我的路线(user-id:1).

但因为我是一个好奇的家伙,我尝试访问/route.aspx?routeid=855哪个属于另一个用户(user-id:2).

如何才能最好地避免人们访问其他人的数据?我应该在每次数据库调用时发送每个用户ID(来自会话),我应该在每次加载页面时验证用户/密码还是最好和最安全的方法是什么?

我希望我说得够清楚.

Dan*_*son 3

不要重新发明轮子

编辑:存储 UserId - 你不必这样做。当然,只要用户登录,您就可以随时从 MembershipProvider 获取它:

MembershipUser user = Membership.GetUser();
Guid UserID = user.ProviderUserKey;
Run Code Online (Sandbox Code Playgroud)

在我看来,您需要实现 ASP.NET 成员资格提供程序。阅读此资源:http://odetocode.com/articles/427.aspx

另外,Scott Guthrie 的精彩系列:http://weblogs.asp.net/scottgu/archive/2006/02/24/ASP.NET-2.0-Membership_2C00_-Roles_2C00_-Forms-Authentication_2C00_-and-Security-Resources-。 ASPX

一般来说,采用这种方法:使用表单身份验证来验证用户是谁。这是安全性的身份验证方面。也就是说,通常使用用户名和密码来确定用户的身份。

安全性的第二部分是授权,一旦您知道用户是谁,就会发生授权。这基本上包括确定经过身份验证的用户可以访问哪些资源。一个成熟的系统将包括以下实体:

User: may contain extended profile information captured on registration
Resource: a page or other resource that can be restricted.
Group: a group of users who can access resources due to their group membership (groups are granted resource access)
Role: a type of user such as Administrator/Developer/Salesperson. 
Run Code Online (Sandbox Code Playgroud)

因此,要授予用户对routeid 854(资源)的访问权限,您可以将资源直接授予用户,或者如果有多个用户应该有权访问该资源并且这些用户形成一个自然组,则创建该组,授予将资源添加到组并将用户添加到组。

然后你可以通过资源 id 访问 User.Resources 或者你可以使用保护整个页面

if(!User.IsInRole("RoleName"))
{
  //redirect to access denied page
}
Run Code Online (Sandbox Code Playgroud)

使用提供者模型有很多好东西。

编辑:如果您决定存储有关用户的个人资料信息,需要注意的事项:ProfileProvider 的默认实现不是特别好。Scott Guthrie 写了一篇关于基于表的提供程序的好文章,该文章更好:http://weblogs.asp.net/scottgu/archive/2006/01/10/435038.aspx