SqlRoleProvider:调用 Roles.GetRolesForUser 时出现 NullReferenceException [英] SqlRoleProvider: NullReferenceException when calling Roles.GetRolesForUser
问题描述
场景:WCF 服务使用 SqlRoleProvider 与 Sql Server 2012 数据库服务器进行身份验证.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:未将对象引用设置为对象的实例.
在 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)
角色管理已启用.
在我的本地开发机器(服务器 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.
要解决这个问题调用:
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屋!