Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Permission Denied When Using "WITH EXECUTE AS"


Permission Denied When Using "WITH EXECUTE AS"

Author
Message
David-155102
David-155102
SSC Veteran
SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)

Group: General Forum Members
Points: 293 Visits: 459
I have a stored procedure that uses "WITH EXECUTE AS" to allow the querying of some system views, the stripped down syntax is as follows:

CREATE PROCEDURE [dbo].[MyTestProc] 


WITH EXECUTE AS 'DOMAIN\UserA'
AS

SET NOCOUNT ON;


SELECT COUNT(*)
FROM msdb.dbo.sysjobs_view;



When I run this procedure I get the error message:

The SELECT permission was denied on the object 'sysjobs_view', database 'msdb', schema 'dbo'

UserA is a member of sysadmin so I would expect it to be able to access the view and in fact if I comment out the "WITH EXECUTE AS" line and run the stored procedure as UserA it works. Any ideas what might be happening?

Thanks
Want a cool Sig
Want a cool Sig
Old Hand
Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)

Group: General Forum Members
Points: 318 Visits: 705
Execute As doesn't go outside the local database without extended authentication from the other database. It's scope is within the local DB. I only recently learned this myself. I'm going through a series of online videos.
Here's where I ran into it:
http://technet.microsoft.com/en-us/sqlserver/gg545013.aspx

---------------------------------------------------------------
Mike Hahn - MCSomething someday:-)
Right way to ask for help!!
http://www.sqlservercentral.com/articles/Best+Practices/61537/
I post so I can see my avatar Hehe
I want a personal webpage Cool
I want to win the lotto :-D
I want a gf like Tiffa w00t Oh wait I'm married!:-D
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45153 Visits: 39923
If "SA" owns the database, you could just execute as OWNER.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
David-155102
David-155102
SSC Veteran
SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)

Group: General Forum Members
Points: 293 Visits: 459
I found the problem but still not sure why it doesn't work!

I used the USER_NAME() and SUSER_NAME() functions to check the before and after and found that SUSERNAME() returned dbo before the switch (EXECUTE AS) but returned Domain\UserA after the switch.

I moved the WITH EXECUTE AS into the procedure itself and changed it to EXECUTE AS USER 'Domain\UserA' but this still failed, however changing it to EXECUTE AS LOGIN 'Domain\UserA' worked.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45153 Visits: 39923
David-155102 (10/24/2012)
I found the problem but still not sure why it doesn't work!

I used the USER_NAME() and SUSER_NAME() functions to check the before and after and found that SUSERNAME() returned dbo before the switch (EXECUTE AS) but returned Domain\UserA after the switch.

I moved the WITH EXECUTE AS into the procedure itself and changed it to EXECUTE AS USER 'Domain\UserA' but this still failed, however changing it to EXECUTE AS LOGIN 'Domain\UserA' worked.


Just checking to make sure... I'd really like to suggest using user names/logins (whatever) for this type of thing unless they are very specifically NOT attached to human entities. They should be setup for this type of proxy only and no human should ever be allowed to login through them. Their password should be guarded as strictly as the SA password should be.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search