我可以在 SQL Server 2008 或 C# 中在运行时限制用户的权限吗?

发布于 2024-11-04 19:32:20 字数 275 浏览 0 评论 0原文

我正在开发一个网站,该网站托管在一个共享托管网站上,该网站为我提供了 1 个数据库连接。

当然,该登录名具有读取和写入的完全访问权限(因为它是我用来构建表等的连接)。

但是,我希望允许用户能够编写临时 SQL 查询,但我想严格限制它们......作为示例,不能写入任何表。

所以我的问题是 - 有没有办法在运行时限制任何功能......也许作为连接字符串中传递的设置?

或者我可以用 C# 做些什么? (例如:我可以启动事务并将其回滚)?

I have a website that I'm working on, and its hosted on a shared hosting site that gives me 1 database connection.

Of course, that login has full access to read and write (as it is the connection I use to build my tables, etc).

However, I want to allow users to be able to write ad-hoc SQL queries, but I want to limit them severely... as an example, not being able to write to any tables.

So my question is - is there ANY way to limit ANY functionality at all at runtime... perhaps as a setting passed in the connection string?

Or is there something I can do in C#? (example: I can start a transaction and roll it back)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋意浓 2024-11-11 19:32:20

您是否有理由无法为每个用户创建新的用户帐户?然后使用内置的服务器安全性来限制每个用户可以执行的操作。

例如,您可以为新的受限用户设置角色组,然后使用表安全性来限制访问。

grant select on my_table to limited_user_group
deny insert, update, delete on my_table to limited_user_group

Is there a reason you can't create new user accounts for each user? Then use the built in server security to limit what each user can do.

You could, for example, set up role groups for the new limited users, and then use table security to limit access.

grant select on my_table to limited_user_group
deny insert, update, delete on my_table to limited_user_group
蛮可爱 2024-11-11 19:32:20

您可以使用 EXECUTE AS t-sql 语句来切换执行上下文用于另一个用户的会话。您可以使用WITH COOKIE 锁定执行上下文,直到您通过REVERT 语句提供cookie 值。

You can use the EXECUTE AS t-sql statement to switch the execution context for the session to another user. You can use WITH COOKIE to lock the execution context down until you supply the cookie value with your REVERT statement.

双手揣兜 2024-11-11 19:32:20

因此,看来您无法限制对与您完全相同的用户的访问...所以我正在做的解决方法是我创建了另一个数据库(一个空白数据库),并且我将包装所有我将强制回滚的事务中的用户查询。

这是一个令人讨厌的解决方案,但正如 Factor Mystic 在他的评论中所说 - “获得更好的托管”可能是最好的解决方案(意思是 - 这里的问题在于托管公司)。

So it appears that you cannot limit access to the very same user that you are... so what I'm doing as a workaround is I've created another database (a blank one) and I'm going to wrap all of the user queries in a transaction that I will force to rollback.

It's a yucky solution, but as Factor Mystic said in his comment - "get better hosting" is probably the best solution (meaning - the problem here lies with the hosting company).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文