2013-04-25 6 views
7

के तहत डेटाबेस "मॉडल" तक पहुंचने में सक्षम नहीं है, मैंने एक संग्रहीत प्रक्रिया बनाई है जो विशेष अनुमतियों की आवश्यकता वाले संचालन की श्रृंखला करता है, उदाहरण के लिए डेटाबेस बनाएं, डेटाबेस पुनर्स्थापित करें, आदि। मैं इस संग्रहित प्रक्रिया कोसर्वर प्रिंसिपल "sa" वर्तमान सुरक्षा संदर्भ

execute as self 

... ताकि यह एसए के रूप में चलता है। ऐसा इसलिए है क्योंकि मैं बिना किसी अनुमति के SQL सर्वर देना चाहता हूं जो केवल इन आदेशों को चलाने की क्षमता है जिन्हें मैंने परिभाषित किया है।

लेकिन जब मैं इस संग्रहीत proc चलाने के लिए, मैं

The server principal "sa" is not able to access the database "model" under the current security context. 

कैसे आ एसए मॉडल डेटाबेस का उपयोग नहीं कर सकते हैं? मैं वास्तव में एसए के तहत संग्रहीत proc में कोड चला गया, और यह ठीक चला गया।

उत्तर

8

जारी रखने से पहले Extending Database Impersonation by Using EXECUTE AS पढ़ें।

अगर USER बयान, के रूप में या खंड रूप में निष्पादित का उपयोग करके एक डेटाबेस-दायरे वाले मॉड्यूल के भीतर निष्पादित का उपयोग करके एक प्रमुख नाम से कार्य, प्रतिरूपण की गुंजाइश डिफ़ॉल्ट रूप से डेटाबेस के लिए प्रतिबंधित है। यह का अर्थ है कि डेटाबेस के दायरे से बाहर की वस्तुओं के संदर्भ एक त्रुटि लौटाएंगे।

आपको module signing का उपयोग करने की आवश्यकता है। Here एक उदाहरण है।

+0

धन्यवाद, मैं जांच करेंगे यह बाहर है। – user884248

0

किसी और यहाँ आने के लिए इस जानकारी की तलाश में के लिए, यहाँ नमूना कोड है कि वास्तव में दर्शाया है जिसे ओ पी (और मैं) (उत्कृष्ट जानकारी पर Remus से ऊपर आधारित) की तलाश है:

-- how to sign a stored procedure so it can access other Databases on same server 
    -- adapted from this very helpful article 
    -- http://rusanu.com/2006/03/01/signing-an-activated-procedure/ 

    USE [master] 
    GO 
    CREATE DATABASE TempDB1 
    CREATE DATABASE OtherDB2 
    GO 
    USE TempDB1 
    GO 
    -- create a user for TempDB1 
    CREATE USER [foo] WITHOUT LOGIN 
    GO 

    CREATE PROCEDURE TempDB1_SP 
    AS 
    BEGIN 
     CREATE TABLE OtherDB2.dbo.TestTable (ID int NULL) 
     IF @@ERROR=0 PRINT 'Successfully created table.' 
    END 
    GO 

    GRANT EXECUTE ON dbo.TempDB1_SP TO [foo] 
    GO 

    EXECUTE AS User='foo' 
     PRINT 'Try to run an SP that accesses another database:' 
     EXECUTE dbo.TempDB1_SP 
     GO 
    REVERT 

    -- Output: Msg 916, Level 14, State 1, Procedure TempDB1_SP, Line 5 
    -- [Batch Start Line 14] 
    -- The server principal "..." is not able to access the database 
    -- "OtherDB2" under the current security context. 

    PRINT '' 
    PRINT 'Fix: Try again with signed SP...' 

    -- Create cert with private key to sign the SP with. 
    -- Password not important, will drop private key 

    USE TempDB1 
    GO 
    -- create a self-signed cert 
    CREATE CERTIFICATE [DB_Cert] 
     ENCRYPTION BY PASSWORD = 'Password1' 
     WITH SUBJECT = 'Signing for cross-DB SPs'; 
    -- Sign the procedure with the certificate’s private key 
    ADD SIGNATURE TO OBJECT::[TempDB1_SP] 
     BY CERTIFICATE [DB_Cert] 
     WITH PASSWORD = 'Password1' 
    -- Drop the private key. This way it cannot be used again to sign other procedures. 
    ALTER CERTIFICATE [DB_Cert] REMOVE PRIVATE KEY 
    -- Copy the public key part of the cert to [master] database 
    -- backup to a file and create cert from file in [master] 
    BACKUP CERTIFICATE [DB_Cert] TO FILE = 'C:\Users\Public\DBCert.cer' 

    USE [OtherDB2] -- or use [master] = all DBs on server accessible 
    GO 
    CREATE CERTIFICATE [DB_Cert] FROM FILE = 'C:\Users\Public\DBCert.cer'; 
    -- the 'certificate user' carries the permissions that are automatically granted 
    -- when the signed SP accesses other Databases 
    CREATE USER [DB_CertUser] FROM CERTIFICATE [DB_Cert] 
    GRANT CREATE TABLE TO [DB_CertUser] -- or whatever other permissions are needed 
    GO 

    USE TempDB1 
    EXECUTE dbo.TempDB1_SP 
    GO 
    -- output: 'Successfully created table.' 
    -- clean up: everything except the cert file, have to delete that yourself sorry 
    USE [master] 
    GO 
    DROP DATABASE TempDB1 
    DROP DATABASE OtherDB2 
    GO 
संबंधित मुद्दे