2009-01-23 59 views
8

在測試中,我使用的數據庫的用戶是一個大jefe。在生產中,他只有執行。ASP.Net Membership.DeleteUser

當我打電話,

Membership.DeleteUser(user) 

在測試中,它的工作。 我嘗試在生產相同的,我得到這個:

The DELETE statement conflicted with the REFERENCE constraint "FK__aspnet_Us__UserI__37703C52". The conflict occurred in database "Testing", table "dbo.aspnet_UsersInRoles", column 'UserId'.

在我seargles(在谷歌搜索),我遇到了這個link 其中傢伙說的話,

Error: The DELETE statement conflicted with the REFERENCE constraint "FK__aspnet_Me__UserI__15502E78". The conflict occurred in database "YourDBName", table "dbo.aspnet_Membership", column 'UserId'.

Took me a while to find a solution to this across multiple sites and options as the error and possible solutions were rather misleading. Turns out, at least in my case, it was a problem with permissions on the membership database. The user I'm using to connect had access to view the membership details within the database itself, but as part of the aspnet_Users_DeleteUser stored procedure it selects from the sysobjects table. The membership connection user apparently did not have sufficient rights to do that select so the overall delete failed.

The fix for me was to add the user to the aspnet_Membership_FullAccess role for the membership database.

但是,當我做到了,它不起作用。任何人有任何想法如何處理這個?

回答

7

一點檢查,我發現這個問題後,該線路aspnet_Users_DeleteUser存儲過程:

IF ((@TablesToDeleteFrom & 1) <> 0 AND 
    (EXISTS (SELECT name FROM sysobjects WHERE (name = N'vw_aspnet_MembershipUsers') AND (type = 'V')))) 

有3條其他類似線等3個表。問題是,如果執行存儲過程的用戶無法訪問vw_aspnet_MembershipUsers,則在從sysobjects中進行選擇時不會啓動。我很想知道爲什麼整個EXISTS語句是必需的。

無論如何,下面的討論「Access to sysobjects to view user tables without having access to the user tables directly in SQL Server Security」都有答案。通過在有問題的視圖上授予「視圖定義」,EXISTS語句現在會成功,您不必在應用程序的連接字符串中爲用戶授予不需要的,不需要的或過多的權限。

2

我相信你的'REFERENCE'約束實際上是一個存在於aspnet_Users表和aspnet_UsersInRoles表之間的數據庫中的外鍵。我想你正在嘗試的用戶在兩個表中都有UserId,並且在你可以從Users表中刪除它之前,它也必須從UsersInRoles表中刪除。

您是否嘗試過http://msdn.microsoft.com/en-us/library/system.web.security.roleprovider.removeusersfromroles.aspx以確保所有角色都從此用戶中刪除?您也可以通過檢查數據庫中這兩個表的行來進行驗證。

4

好的,你猜怎麼着?我這樣說的: http://forums.asp.net/t/1254087.aspx

Ok, few minutes after sending my post I found the solution :) It turns out that SELECT PERMISSION had to be added for ASPNET user on vw_aspnet_MembershipUsers view.

But it is still mystery why I didn’t get an error concerning lack of permission. EXIST statement was just returning false.

,給生產用戶SELECT權限,瞧! 它的工作原理! 謝謝你們!

+0

這沒有任何意義..如果用戶對存儲過程aspnet_Users_DeleteUser具有執行權限,則視圖上的SELECT權限應該沒有關係。就像在任何aspnet表上擁有DELETE權限並不重要。 – Matt 2010-07-07 18:37:57

1

如果在授予vw_aspnet_MembershipUsers上的ASP用戶SELECT後仍然存在錯誤(或類似錯誤),則可能需要爲其他某些vw_aspnet授予SELECT?也是意見。特別是「個人資料」和「UsersInRoles」。否則 - 由於某些原因,DeleteUser SP在從這些視圖中選擇時會得到一個空結果,並拒絕首先從它們中刪除現有條目。

5

我也有這個問題,它是由缺少的意見,以糾正我剛剛使用從另一個數據庫創建腳本,並重新創建所有的vw_aspnet_ *視圖。

0

也許更好,以確保執行刪除成員資格的用戶具有更正ASP.NET成員資格sql角色。 在我的情況下,我刪除了具有一些角色和配置文件屬性的membershipuser。刪除方法失敗,但分配正確的SQL角色後,它的工作。

ALTER ROLE [aspnet_Profile_FullAccess] ADD MEMBER [<YOUR SQL USER>] 
ALTER ROLE [aspnet_Roles_FullAccess] ADD MEMBER [<YOUR SQL USER>] 

如果您正在使用該功能,還可以添加[aspnet_Personalization_FullAccess]。

0

我解決了這個問題,通過刪除proc中的行來檢查視圖。我沒有任何asp成員資格視圖,並且在任何地方都不需要它們,所以創建視圖看起來毫無意義,只是爲了使代碼行可以返回true - proc實際上並不使用視圖。也許如果你使用更多的會員對象功能,你可能需要其他的視圖。無論哪種方式檢查視圖的存在似乎是一個奇怪的方式讓proc決定aspnet_membership表是否有一個它需要刪除的行。

IF ((@TablesToDeleteFrom & 1) <> 0 
    ) 
    --AND 
    -- (EXISTS (SELECT name FROM sysobjects WHERE (name = N'vw_aspnet_MembershipUsers') AND (type = 'V'))))