Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 497213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:45:36+00:00 2026-05-13T05:45:36+00:00

I need to set up a system which will allow developers to request an

  • 0

I need to set up a system which will allow developers to request an emergency ID for a database. They will be assigned to a role called ‘analyst’ which will provide them a drop down box with the databases they can gain access to. They will submit the request and a temporary SQL Login will be generated and displayed on screen. The login will have some elevated privs. The login will be removed after 12 hours.

I’ve got the whole thing working myself as an SA on ASP.net, but now I’m working on modifying the procedures to work using a SQL Login in the application connection string.

I’ve tried a few things to get it working, but have run into a roadblock.

Here’s my procedure that does the real work.

    USE [SQLEmergencyLoginRequest]
GO
/****** Object:  StoredProcedure [dbo].[SQLELR_Login_CREATE]    Script Date: 12/08/2009 14:48:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  PROCEDURE [dbo].[SQLELR_Login_CREATE] 
@SERVER VARCHAR(50),
@DATABASE VARCHAR(50),
@NTLOGIN VARCHAR(50),
@IR INT,
@LOGIN VARCHAR(50) OUTPUT,
@PWD VARCHAR(20) OUTPUT,
@NotifyDBA INT 
WITH EXECUTE AS OWNER 
AS
/*
Emergency_Access_Login_CREATE:  Create Login/PWD, Create User, Create Role, Add User to Role, return Login/PWD.
*/
DECLARE @Random_Login_Extension VARCHAR(20)
DECLARE @sql VARCHAR(1000)

SET @Database = QUOTENAME(@Database);


BEGIN TRANSACTION

--CREATE LOGIN/PWD
EXEC dbo.random_password @Random_Login_Extension OUTPUT;
EXEC dbo.random_password @PWD OUTPUT;

SET @LOGIN = 'Emergency_Login_' + @Random_Login_Extension;

SET @sql= 'CREATE LOGIN [' + @LOGIN + ']' +
'WITH PASSWORD= ''' + @PWD + ''', DEFAULT_DATABASE=[master], ' +
'CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF';

EXEC(@sql);

--CREATE USER
DECLARE @User_Cmd VARCHAR(1000);

SET @User_Cmd = 'USE ' + @DATABASE + ';' +

'CREATE USER [' + @LOGIN + '] FOR LOGIN [' + @LOGIN + '];' +
'EXEC sp_addrolemember N''db_datareader'',''' + @LOGIN + ''';' +
'EXEC sp_addrolemember N''db_datawriter'',''' + @LOGIN + ''';' +
'EXEC sp_addrolemember N''db_ddladmin'',''' + @LOGIN + ''';';

EXEC (@User_Cmd);

INSERT INTO dbo.SQLELR_Emergency_Logins
           ([CreationTime]
           ,[NTLogin]
           ,[IR]
           ,[SERVER]
           ,[DATABASE]
           ,SQLLoginCreated)
     VALUES
           (GETDATE()
           ,@NTLOGIN
           ,@IR
           ,@SERVER
           ,@DATABASE
           ,@LOGIN)


DECLARE @MYBODY VARCHAR(500)
SET @MYBODY = @NTLOGIN + ' has created a temporary login in the ' + @Database + ' Database.  The login name is ' + @LOGIN;

DECLARE @MYSUBJECT VARCHAR(500) 
SET @MYSUBJECT = 'Emergency Login Creation ON server ' + @@SERVERNAME;

IF @NotifyDBA = 1
BEGIN 
    EXEC msdb.dbo.sp_notify_operator
       @profile_name = 'SQLDBA',
       @name = 'SQLDBA',
       @subject = @MYSUBJECT,
       @body = @MYBODY;
END 

COMMIT TRANSACTION

I don’t want the application account to be highly privileged in every DB, so I created another account which will go into every db and have db_owner. Evidently the sp_addrolemember using fixed db roles needs a db_owner to work, which is why the acct is db_owner. I’d prefer security admin, but it seems it’s not possible.

Back to the problem – using EXECUTE AS with dynamic code does not work.

Is the only way to get this done by creating a stored procedure in every database which creates the user?

We’re doing this because we’d like to crank down security on this server and take away db_owner from developers which has been the norm for years. Creating this mechanism will satisfy their only remaining complaint about not having access. They are afraid we won’t answer a page and they will be unable to resolve an issue, so this will take care of that.

Of course, any advice on security holes here would be appreciated as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:45:36+00:00Added an answer on May 13, 2026 at 5:45 am

    The EXECUTE AS clause on the work procedure puts you into the ‘execute as’ cage, see Extending Database Impersonation by Using EXECUTE AS. Because the EXECUTE AS of the procedure is an database principal, the execute as context will be trusted only inside the database.

    There are two workarounds, the 500lb sledge hammer of ALTER DATABASE [SQLEmergencyLoginRequest] SET TRUSTWORTHY ON or the surgical precission tool of code signing, see Call a procedure in another database from an activated procedure for an example. I highly recommend the code signing approach:

    • craete a certificate in SQLEmergencyLoginRequest
    • sign the procedure
    • drop the private key of the certificate to prevent future use for signing
    • export the certificate
    • import the certificate in master
    • create a login derived from the certificate
    • grant AUTHENTICATE on SERVER to the certificate derived login
    • grant all other priviledges needed for the procedure to this derived login

    This would ensure that the procedure has all the needed priviledges to do its work, in any database. You have to redo the whole signing procedure every time you alter it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to set up a system in which user uploaded files will
I need to set up a continuous integration system. We use ClearCase version control
I need Set collection, where its items will be identified by items class. Something
I'm developing an add on to a system I have that will allow my
I'm creating an online poll from scratch which will be held in a database.
I have a windows service running as local system, which will search for some
I'm trying to write an extension method that will allow me to set focus
I am currently in the process of designing a system which will use multiple
I need to create vbscript which will create new folder 'test' and subfolder 'Output'.There
I will need to supply a large data set consisting of numbers to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.