通过WCF暴露sql server主键
我通过 WCF 公开 Sql Server 数据以供 WP7 应用程序使用。现在我有一个选择,要么使用主键处理数据,要么使用同一个表的其他列。
我个人认为最好不要暴露PK,不是手机不安全,而是因为互联网。但是,请给我建议。
谢谢,
I am exposing Sql Server data through WCF to be consumed on a WP7 application. Right now I have a choice, either I work with the data using the primary key or other columns for the same table.
I personally think it is best not to expose the PK, not that the phone is not secure, but because of the internet. But, please advice me.
thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您提到的 PK 是 int 类型,公开这些值是不安全的。
在本例中,我将添加 GUID 类型(SQL Server 中的唯一标识符)的备用键。可以使用 .Net 代码中的
System.Guid.NewGuid()
方法或使用 SQL 中的newid()
函数填充该字段。这将使密钥变得不可预测。例如,如果某人嗅探您的 WCF 流量并看到值为 4 的 PK,他们可以尝试接近值 (2,3,5...) 来访问其他记录。 GUID 不是连续的,因此很难猜测其他关键值。
请注意,GUID 占用的存储空间是 int 的 4 倍(或 bigint 的 2 倍),因此如果这是一个大型表,则 GUID 方法可能会占用更多的空间。
在这种情况下,您可能会重新考虑暴露密钥是否真的是一个安全问题;如果应用程序的设计是安全的,或者如果所有记录都可供任何人查看,那么暴露 PK 值应该不会那么糟糕。
毕竟,如果您查看此页面的 URL,您会注意到值
8690786
,它很可能是问题的公开整数标识符。I assume that the PK you mentioned is of type int and it would not be secure to expose the values.
In this case I'd add an alternate key of type GUID (uniqueidentifier in SQL Server). The field can be populated using the
System.Guid.NewGuid()
method from .Net code, or using thenewid()
function in SQL.That would make the key not predictable. For example, if a person sniffs your WCF traffic and sees a PK with a value of 4, they can try close values (2,3,5...) to access other records. GUIDs are not sequential, so it's really hard to guess other key values.
Please note that GUIDs take up 4x more storage space than an int (or 2x bigint), so if this will be a large table, the GUID approach could take considerably more space.
In this case, you might reconsider if exposing keys is really a security problem; if the app is designed to be secure, or if all the records are meant to be viewed by anyone then exposing the PK value should not be so bad.
After all, if you look at the URL of this page you will notice the value
8690786
, which most probably is an exposed integer identifier of the question.