Is it possible to get "NT AUTHORITY\NETWORK SERVICE" user independent of language? Is it possible to get "NT AUTHORITY\NETWORK SERVICE" user independent of language? sql-server sql-server

Is it possible to get "NT AUTHORITY\NETWORK SERVICE" user independent of language?


Yes, it is possible. To solve the issue of making sure that your are referencing the NT Authority\Network Service account independent of the OS language, there is a reference at https://support.microsoft.com/en-us/help/243330/well-known-security-identifiers-in-windows-operating-systems which identifies the SID of this account. It is defined as

SID: S-1-5-20Name: NT AuthorityDescription: Network Service

Note: the numbers seem to be expressed as decimal numbers.

If in your SQL Server management studio you select from sys.server_principals:

select * from sys.server_principals

you'll see that NETWORK SERVICE has a SID value of 0x010100000000000514000000the '514' (this is hexa) part corresponds to the 5-20 (decimal).

If you check the following statement in a query window:

select quotename(SUSER_SNAME(0x010100000000000514000000))

you'll see the result: [NT AUTHORITY\NETWORK SERVICE]

With this in hand, your original creation statement becomes:

DECLARE @user nvarchar(50)DECLARE @SQLStatement nvarchar(500)SET @user = quotename(SUSER_SNAME(0x010100000000000514000000));SET @SQLStatement =N'IF NOT EXISTS(SELECT principal_id FROM sys.database_principals WHERE name = ''NT AUTHORITY\NETWORK SERVICE'')  BEGIN      CREATE USER [NT AUTHORITY\NETWORK SERVICE] FOR LOGIN ' + @user + N' WITH DEFAULT_SCHEMA=[dbo]      ALTER ROLE [db_owner] ADD MEMBER [NT AUTHORITY\NETWORK SERVICE]  END'EXEC sp_executesql @SQLStatement;

And you'll get the desired created account.

Cheers.


Don't use NT service account.

Create a local windows or domain account. Add it as login to SQL Server and as user to database you need. And change your windows service application to run under that account.