2017-02-14 56 views
0

我的角色提供:C#角色多個供應商傾斜通話功能

<roleManager enabled="true" cacheRolesInCookie="true" cookieName=".ASPROLES" defaultProvider="AspNetSqlRoleProvider"> 
     <providers> 
     <clear/> 
     <add connectionStringName="ConnectionString1" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider,System.Web, Version=2.0.0.0, Culture=neutral,PublicKeyToken=xxx"/> 
     <add connectionStringName="ConnectionString2" applicationName="/" name="AspNetSqlRoleProviderProduction" type="System.Web.Security.SqlRoleProvider,System.Web, Version=2.0.0.0, Culture=neutral,PublicKeyToken=xxx"/> 
     </providers> 
    </roleManager> 

當我嘗試調用AddUserToRole:

var roleProviderProduction = Roles.Providers["AspNetSqlRoleProviderProduction"]; 
roleProviderProduction.AddUserToRole(username, role); 

我得到這個錯誤:

Error 59 'System.Web.Security.RoleProvider' does not contain a definition for 'AddUserToRole' and no extension method 'AddUserToRole' accepting a first argument of type 'System.Web.Security.RoleProvider' could be found (are you missing a using directive or an assembly reference?)

+0

不叫'AddUsersToRoles'的方法? – wkl

+0

否:https://msdn.microsoft.com/en-us/library/system.web.security.roles.addusertorole%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 – atroul

+0

您正在混淆[RoleProvider](https://msdn.microsoft.com/en-us/library/system.web.security.roleprovider(v = vs.110).aspx)和[Roles](https://msdn.microsoft。 COM/EN-US /庫/ system.web.security.roles(v = vs.110)的.aspx)。請參閱下面的答案。 – wkl

回答

0

你混合up RoleProviderRoles。雖然Roles中有AddUserToRoleRoleProvider只有AddUsersToRoles

您應該使用

var roleProviderProduction = Roles.Providers["AspNetSqlRoleProviderProduction"]; 
roleProviderProduction.AddUsersToRoles(new string[]{username}, new string[]{role}); 

Roles.AddUserToRole(username, role); 
+0

爲什麼? AddUserToRole與AddUserToRole不是同一個函數。爲什麼我不能將AddUserToRole與特定的(不是默認的)角色提供程序一起使用?對不起,我對此很陌生 – atroul

+0

因爲它沒有在'RoleProvider'類中定義。毫無疑問,微軟可能會爲單一角色的單一用戶定義一種方法,但他們顯然決定不這樣做。無論如何,將參數封裝到'string []'並調用'AddUsersToRoles'是沒問題的。 – wkl