9

I've been trying to find why, how and where to use Users Without Login and I didn't find my exact question or anything that gets closer. Where I did my research (I also read other posts, saw Youtube Videos):

I was searching for ways to turn my db more secure and I recently came to know that there's an option to create user without login. I thought it is nice. But I didn't understand how to use it (not create, use), then after my research I realized that this user accesses the db while impersonating another user/role. I thought it would be great to not have a user and somehow access the db. But when I saw that there's something called execute as I thought that it wasn't useful because why I would execute queries on this "login-less" user as it was another user? Why I just don't use this user's permission? Then I thought maybe this user has too much permissions, so I just create a new one with less permissions...

Okay, I don't know why you would create a user without login. How should this be used? It's stupid, but I tried to login and failed (sorry, but I'm used to login with users, I never thought on creating a user and not to login, I just know that users login to something, that's where my knowledge is).

So, I don't know why to create this type of user instead of a normal one? I don't know how (like do I login, do I just type the query and to use it)? I don't know where (like, do I need to make an C# app, do I use command...) to use?

My knowledge only takes me till here, I don't have a Senior DBA to ask. Please explain in simple words if possible.

2 Answers 2

9

Imagine that you have somebody running an app, and this requires certain privileges. You do want to be able to log who is doing what and see who are logged in to your SQL Server. So each person need their own login (this can possible be one login created for a domain group).

You don't feel comfortable having these people connecting using SSMS and play around in the database. Since the app might need DELETE permissions on the Customers table (say), then the end-user now can do this from SSMS.

So you create a user without login and have the app use EXECUTE AS as this user. I.e., without EXECUTE AS, the end-user have no privileges at all in the database (i.e., the potential AD group you created a login for and its subsequent user in the database). But they do have privileges when they've done EXECUTE AS.

Is this read security? No, of course not. But perhaps better than either having no traceability or having the end-users being able to play around in the database "directly".

What about application roles? Yes, that can be tighter since they are password protected.

Another usage for this is when you are testing things, using EXECUTE AS USER. You want to play around with something permissions related and it is easier to assign the permissions to a user without login and then do your tests then having to create a login and a user for whatever you want to test.

5
  • How do I test the without login user? Like if I'm coding a C# console app, how I use this user? Commented Mar 24, 2021 at 15:40
  • 1
    You login as you usually do. I.e., you need a login and a user, but then you switch user using EXECUTE AS USER. Commented Mar 25, 2021 at 10:43
  • So this would not be a good idea for a powershell script on a user's laptop since the user could see the connection string if they are smart enough to open the file in notepad. Also, even if it is an exe, the connection string would need to be obfuscated for the same reason. So really you'd need to use a login that has only the permissions which the app needs, which negates the need for a user sans login.
    – tolsen64
    Commented Dec 27, 2021 at 18:43
  • The login/user in the connection string can have super low privileges. But the app will do EXECUTE As and this would be in the power shell script. Application roles are a bit better, but the app will submit the sp_setapprole proc and pass the password for it. I.e. it is at least owd protected, where execute as user only need to know the name of the user and you can do the same. Commented Dec 27, 2021 at 19:23
  • 1
    For future me and others, the syntax for this command is execute as user='user' Commented Nov 17, 2022 at 21:42
4
  1. User without logins are needed when you sign stored procedure with a certificate

You create certificate inside user database, create user from certificate, grant permissions to this user, and sign stored procedure by certificate

This is needed for stored procedure to obtain required permissions, for example update or delete on particular table. Then you grant end-user permission to execute stored procedure. Result is that end-user can NOT update or delete from table directly, but using stored procedure, he can.

Stored procedure can have logic that allows to limit how many rows can be deleted/updated per 1 execution, or which exactly rows, do some checkups, etc. etc.

  1. User without logins sometimes used in "Execute as" clause in a stored procedure or a script, so SP obtains permissions similar to what I explained above with certificate signing
  1. User without logins can be temporarily created just to test some permissions by the DBA using "execute as user" clause (Tibor already explained in his answer)

Not the answer you're looking for? Browse other questions tagged or ask your own question.