SqlRoleProvider:调用Roles.GetRolesForUser时出现NullReferenceException [英] SqlRoleProvider: NullReferenceException when calling Roles.GetRolesForUser
问题描述
场景:
使用SqlRoleProvider进行Sql Server 2012数据库服务器身份验证的WCF服务。 WCF托管在IIS7网络服务器上。
Scenario: WCF Service using SqlRoleProvider for authentication with a Sql Server 2012 Database server. WCF is hosted on a IIS7 webserver.
请看到此错误:
System.NullReferenceException:对象引用未设置为对象的实例。
at System.Web.Security.Roles.GetRolesForUser(String username)
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.Security.Roles.GetRolesForUser(String username)
已启用RoleManagement。
RoleManagement is enabled.
在我的本地开发机器(服务器2012,iis7)上,这可以正常工作。当我登录并调用该方法时,将检索角色。
On my local development machine (server 2012, iis7) this works fine. When I log in and call the method the roles are retrieved.
在另一台服务器(测试环境)上,它无法正常工作。我可以登录(用户通过用户身份验证并传递给sql server数据库)但是当我尝试检索该用户的角色时,我得到一个nullreferenceexception。
On the other server (test environment) it's not working. I can login (user is authenticated with user and pass against sql server database) but when I try and retrieve the roles for that user, I get a nullreferenceexception.
如何这可能,有没有人有任何线索可以解决这个问题?
How is this possible, does anyone have any leads on to where this problem might occur?
最好的问候。
推荐答案
使用标签搜索Stack Overflow,我发现这个网站:
http://www.lhotka.net/weblog/CallingRolesGetRolesForUserInAWCFService.aspx
Googling with the tags Stack Overflow provided I came across this site: http://www.lhotka.net/weblog/CallingRolesGetRolesForUserInAWCFService.aspx
简而言之:
显然在.net 3.5和.net 4之间出现了问题。
In short: apparently something broke between .net 3.5 and .net 4.
要解决此问题,请致电:
To solve this issue call:
string[] roles = Roles.Provider.GetRolesForUser(ServiceSecurityContext.Current.PrimaryIdentity.Name);
而不是
string[] roles = Roles.GetRolesForUser(ServiceSecurityContext.Current.PrimaryIdentity.Name);
区别在于 .Provider
在中间添加。添加后,它工作正常。
The difference is in the .Provider
which is added in the middle. After adding this it worked fine.
这篇关于SqlRoleProvider:调用Roles.GetRolesForUser时出现NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!