简体   繁体   English

通过在服务帐户下运行的Web服务将用户名从C#本机应用程序传递到SQL Server的好方法

[英]Good ways to pass user name from C# native app to SQL Server via a web service running under a service account

Our company has a model where all Web services run under its own specific service account. 我们公司有一个模型,其中所有Web服务都在其自己的特定服务帐户下运行。 for example: comp\\Ws.App 例如:comp \\ Ws.App

This Web service does all the Database interaction using Store procedures to a SQL database and the service account has all the rights to do so. 该Web服务使用存储过程对SQL数据库执行所有数据库交互,并且该服务帐户具有这样做的所有权利。

We have a native client built written in C# that uses the web service for all data related queries. 我们有一个用C#编写的本地客户端,该客户端使用Web服务进行所有与数据相关的查询。

My problem is that we have audits on these database tables using triggers which record the user using the USER_NAME() build in SQl function. 我的问题是,我们使用触发器对这些数据库表进行审核,这些触发器使用SQl函数中内置的USER_NAME()记录用户。

Is there any way that the name of the user running the Native app gets passed in to a SQL build in function? 运行本机应用程序的用户名是否可以通过任何方式传递给SQL内置函数? It does not have to be the USER_NAME() function, but any built in function is good enough. 它不必是USER_NAME()函数,但是任何内置函数都足够。

What I am trying to avoid is the following: 我想避免的是以下情况:

  • Change every function to pass the user name 更改每个功能以传递用户名
  • Change every open connection statement to pass the user name 更改每个打开的连接语句以传递用户名

Currently what I do is change the Application Name property in the SQL connection string to include the user identity and strip this from the APP_NAME() built in SQL function 当前,我要做的是更改SQL连接字符串中的“ 应用程序名称”属性以包括用户身份,并将其从SQL函数内置的APP_NAME()中删除

Notes 笔记

  • Users do not have direct access to SQL Server 用户没有直接访问SQL Server的权限

Any other good solutions? 还有其他好的解决方案吗?

One option is to store the user id in the SQL Server context info. 一种选择是将用户ID存储在SQL Server上下文信息中。 As soon as the web service identifies or authenticates the user, then the web service stores the user id in the context info, this can be done by calling a stored procedure like the following: 一旦Web服务标识或认证了用户,然后Web服务便将用户ID存储在上下文信息中,这可以通过调用如下存储过程来完成:

CREATE PROCEDURE SetUserContext
    @UserId int
AS
    DECLARE @uc binary(4)
    SET @uc = CONVERT(binary(4), @UserId)
    SET CONTEXT_INFO @ci

Then, audit trail triggers read the user id from the context info using a code like this one: 然后,审核跟踪触发器使用如下代码从上下文信息中读取用户ID:

DECLARE @UserId int
SET @UserId = CONVERT(int, SUBSTRING(CONTEXT_INFO(), 1, 4))

Of course you need to keep the connection open after calling SetUserContext, if you close the connection the context info is lost. 当然,您需要在调用SetUserContext之后保持连接打开,如果关闭连接,则上下文信息将丢失。 So the sequence of actions should be as follows: 因此,操作顺序应如下:

  1. Open connection 打开连接
  2. Call SetUserContext 呼叫SetUserContext
  3. Call other stored procedures 调用其他存储过程
  4. Close connection 紧密连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从使用C#中的系统帐户运行的服务中查找域中的用户全名 - how to find user full name in domain from service running with system account in c# 通过C#Web服务将大量数据从SQL Server流式传输到Excel文件 - Streaming large amounts of data from sql server to an Excel file via c# web service 如何从客户端应用程序获取运行Web服务的服务器的SSL证书? - C#.NET - How to get the SSL certificate of a server running a web service from the client app? - C# .NET C#Web服务从SQL Server获取数据 - C# web service get data from sql server 如何在本地系统帐户下禁止C#服务启动? - How to forbid C# service from start under LocalSystem account? 是否可以从C#应用程序访问SQL Server Service Broker? - Is it possible to access SQL Server Service Broker from a C# app? 如何获得SQL Server服务与C#一样运行的帐户? - How to get the account the SQL Server service runs as with C#? 如何在C#应用程序中保护服务帐户? - How to secure service account in c# app? 在应用程序池下运行的WCF服务模拟域用户帐户将一个域帐户打印到网络打印机,而不是另一个域帐户 - WCF Service running under app pool impersonating domain user account prints to network printer for one domain account but not another 通过Web服务将数据从Sql Server移动到Excel - Moving data from Sql Server to Excel via a Web Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM