积极答复者
三请 KeFang Chen 帮忙,关于 Sys.Services.RoleService.load

问题
-
有点繁琐,请您耐心往下看!
我使用Sys.Services.AuthenticationService.login进行用户身份验证。一切正常。(用户名:heywap 角色:admin users)
在ScriptManager中有以下的属性设置:
<RoleService LoadRoles="true" />
登录成功后,我测试这个RoleTest()脚本函数,在id是 txtRole 的 text中输入 users ,提示flase,即表明heywap用户不属于 users角色,实际上该用户肯定是属性该角色。
现在,我刷新该页(F5键),再重复上面的测试,则提示 true。
查MSDN文档, LoadRoles="true" 有如下信息:
在 ScriptManager 控件或关联的 ScriptManagerProxy 控件中,可以指定 LoadRoles 属性。对于代理实例,如果 LoadRoles 设置为 true,则将检索角色列表并将其添加到页中。
也就是说,用户登录成功后,手动刷新或在登录方法
Sys.Services.AuthenticationService.login中将redirectUrl属性设置为 window.location.href也能达到目的,即用户成功登录后,转向到当前页,即达到刷新该页的目的。
但我需要的是,不用刷新也能够得到该用户的角色。
所以,我只好在用户登录成功的回调函数中,加入以下的代码:
Sys.Services.RoleService.load,即下面代码中被注释的那一行。
结果在运行到这个 RoleLoadCompleted 回调函数时,出现以下错误:
消息: Sys.InvalidOperationException: 尚未设置 Web 服务的路径。
行: 4723
字符: 21
代码: 0
URI: http://localhost:4898/ScriptResource.axd?d=9SGM-S12yHm139rn5UbUMKJRMJM3ILNWcuKEVOiSBl01QfvzACogp-eVudnl-o31Njbs0Cy3sATTDaVXToN7n-FRCH_H_UqGiuh3c_hTJ2g1&t=7f3c5983
根据上述信息,应该是ScriptManager中的RoleService这个节点缺少Path的属性和值,但据我所知,Path仅用来需要进行自定义角色验证时,对于内置的AspNetSqlRoleProvider 是不需要设置的,对此我十分不解。请问怎么做,才能在登录成功后,不刷新页面,使这个函数(Sys.Services.RoleService.isUserInRole)的执行结果正确。谢谢。<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="testWebProject.login" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="sm" runat="server"> <RoleService LoadRoles="true" /> </asp:ScriptManager> <script type="text/javascript"> Sys.Application.add_init(OnInit); function OnInit() { Sys.Services.AuthenticationService.set_defaultLoginCompletedCallback(LoginCompleted); Sys.Services.AuthenticationService.set_defaultLogoutCompletedCallback(LogoutCompleted); Sys.Services.AuthenticationService.set_defaultFailedCallback(Failed); } function pageLoad(sender, args) { if (window.location.href.indexOf("ReturnUrl") > -1) { window.alert("您没有权限访问,请切换账号进行登录"); } if (Sys.Services.AuthenticationService.get_isLoggedIn()) { showLoginUserInfo(); } } function RoleLoadCompleted(result, userContext, methodName) { window.alert(result.length); } function showLoginUserInfo(userName) { var strUserName = userName ? userName : '<%=Page.User.Identity.Name %>'; $get("loginUserName").innerHTML = String.format("欢迎您{0}登录", strUserName); $get("loginPanel").style.display = "none"; $get("logoutPanel").style.display = "block"; } function login() { Sys.Services.AuthenticationService.login($get("txtusername").value, $get("txtpassword").value, true, null, null); } function logout() { Sys.Services.AuthenticationService.logout(); } function LoginCompleted(validCredentials,userContext,methodName) { if (validCredentials) { showLoginUserInfo($get("txtusername").value); //Sys.Services.RoleService.load(RoleLoadCompleted, Failed); } else { window.alert("用户名或密码错误"); } } function LogoutCompleted(result, userContext, methodName) { } function Failed(error, userContext, methodName) { window.alert(error.message); } function RoleTest() { window.alert(Sys.Services.RoleService.isUserInRole($get("txtRole").value)); } </script> <%=System.DateTime.Now.ToString() %> <div id="loginPanel"> username:<input type="text" id="txtusername" /> password:<input type="text" id="txtpassword" /><input type="button" value="登录" onclick="login()" /> </div> <div style="display:none" id="logoutPanel"> <span id="loginUserName"></span> <a href='admin/index.aspx'>管理中心</a> <input type="button" value="退出" onclick="logout()" /> <br /><input type="text" id="txtRole" /><input type="button" onclick="RoleTest()" value="Roles" /> </div> </form> </body> </html>
答案
-
试试在web.config中加入下列代码试试。
<system.web.extensions> <scripting> <webServices> <authenticationService enabled="true" /> </webServices> </scripting> </system.web.extensions>
http://msdn.microsoft.com/zh-cn/dd315425.aspx
Microsoft Online Community Support
还有<roleService enabled="true"/>- 已标记为答案 KeFang Chen 2009年10月22日 1:48
-
Hi,
在web.config文件中把roleservice配置加上试试。我这边测试好了。
<system.web.extensions> <scripting> <webServices> <authenticationService enabled="true" /> <roleService enabled="true" /> </webServices> </scripting> </system.web.extensions>
Microsoft Online Community Support- 已标记为答案 heywap 2009年10月21日 14:10
全部回复
-
<RoleService LoadRoles="true" />是自定义角色服务用的 一般不需要
你有几个方面理解错了 第一 验证用户名和验证角色 生命时期不同 一般来说先验证用户名然后验证角色
第二 登陆成功后需要刷新一下才验证角色为true 因为浏览器缓存问题 异步登陆没有刷新浏览器缓存造成的验证角色不成功
修改一下 RoleTest() 方法<asp:ScriptManager ID="sm" runat="server"> </asp:ScriptManager> <script type="text/javascript"> Sys.Application.add_init(OnInit); function OnInit() { Sys.Services.AuthenticationService.set_defaultLoginCompletedCallback(LoginCompleted); Sys.Services.AuthenticationService.set_defaultLogoutCompletedCallback(LogoutCompleted); Sys.Services.AuthenticationService.set_defaultFailedCallback(Failed); } function pageLoad(sender, args) { if (window.location.href.indexOf("ReturnUrl") > -1) { window.alert("您没有权限访问,请切换账号进行登录"); } if (Sys.Services.AuthenticationService.get_isLoggedIn()) { showLoginUserInfo(); } } function RoleLoadCompleted(result, userContext, methodName) { window.alert(result.length); } function showLoginUserInfo(userName) { var strUserName = userName ? userName : '<%=Page.User.Identity.Name %>'; $get("loginUserName").innerHTML = String.format("欢迎您{0}登录", strUserName); $get("loginPanel").style.display = "none"; $get("logoutPanel").style.display = "block"; } function login() { Sys.Services.AuthenticationService.login($get("txtusername").value, $get("txtpassword").value, false, null, null); } function logout() { Sys.Services.AuthenticationService.logout(); } function LoginCompleted(validCredentials, userContext, methodName) { if (validCredentials) { showLoginUserInfo($get("txtusername").value); //Sys.Services.RoleService.load(RoleLoadCompleted, Failed); } else { window.alert("用户名或密码错误"); } } function LogoutCompleted(result, userContext, methodName) { } function Failed(error, userContext, methodName) { window.alert(error.message); } function RoleTest() { Sys.Services.RoleService.load(rolescompleted); } function rolescompleted() { window.alert(Sys.Services.RoleService.isUserInRole($get("txtRole").value)); } </script> <%=System.DateTime.Now.ToString() %> <div id="loginPanel"> username:<input type="text" id="txtusername" /> password:<input type="text" id="txtpassword" /><input type="button" value="登录" onclick="login()" /> </div> <div style="display:none" id="logoutPanel"> <span id="loginUserName"></span> <a href='admin/index.aspx'>管理中心</a> <input type="button" value="退出" onclick="logout()" /> <br /><input type="text" id="txtRole" /><input type="button" onclick="RoleTest()" value="Roles" /> </div>
-
试试在web.config中<system.web>节点后加入下列代码试试。
<system.web.extensions> <scripting> <webServices> <authenticationService enabled="true" /> </webServices> </scripting> </system.web.extensions>
详细情况可以参考下面的链接。
http://msdn.microsoft.com/zh-cn/dd315425.aspx
Microsoft Online Community Support -
试试在web.config中加入下列代码试试。
<system.web.extensions> <scripting> <webServices> <authenticationService enabled="true" /> </webServices> </scripting> </system.web.extensions>
http://msdn.microsoft.com/zh-cn/dd315425.aspx
Microsoft Online Community Support
还有<roleService enabled="true"/>- 已标记为答案 KeFang Chen 2009年10月22日 1:48
-
那就非常奇怪了,我的前台代码就是你的,除了web.config可能不同。运行正常。使用IE7+Framework3.5测试的.
使用的是默认的membership数据库,web.config中关于这个role manager的相关配置如下:
<connectionStrings> <add name="AspNetSqlMembershipConnectionString" connectionString="Data Source=localhost;Initial Catalog=AspNetSqlMembership;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings> <system.web> <authentication mode="Forms"> <forms loginUrl="AuthenticationServiceDemo.aspx" defaultUrl="~/Default.aspx"> </forms> </authentication> <authorization> <deny users="?"/> <allow users="*"/> </authorization> <roleManager defaultProvider="AspNetSqlMembershipProvider" enabled="true"> <providers> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="AspNetSqlMembershipConnectionString" applicationName="test" /> </providers> </roleManager> <membership> <providers> <clear/> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="AspNetSqlMembershipConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="3" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="test" /> </providers> </membership> </system.web> <system.web.extensions> <scripting> <webServices> <authenticationService enabled="true" /> </webServices> </scripting> </system.web.extensions>
你对照着我的配置试试。
Microsoft Online Community Support -
是这样,并非我的代码不能运行。
我成功登录后(不刷新页面),点击按钮,测试这个函数 RoleTest() 时,结果返回 false.
如果我登录成功后,手动刷新,再点击按钮,测试,函数结果返回 true.
我的本意是不想登录成功后手动刷新或通过程序刷新。而能立即获取当前登录用户的角色,即 RoleTest() 函数返回 true.
所以,我在登录成功的回调函数中,加入下面注释的那行。以立即加载用户的角色,问题就出现在这里。
function LoginCompleted(validCredentials,userContext,methodName)
{
if (validCredentials)
{
showLoginUserInfo($get("txtusername").value);
//Sys.Services.RoleService.load(RoleLoadCompleted, Failed); ///////// 这一行,在用户登录成功后,加载角色
}
else
{
window.alert("用户名或密码错误");
}
}
提示这样的错误:
消息: Sys.InvalidOperationException: 尚未设置 Web 服务的路径。
行: 4723
字符: 21
代码: 0 -
Hi,
在web.config文件中把roleservice配置加上试试。我这边测试好了。
<system.web.extensions> <scripting> <webServices> <authenticationService enabled="true" /> <roleService enabled="true" /> </webServices> </scripting> </system.web.extensions>
Microsoft Online Community Support- 已标记为答案 heywap 2009年10月21日 14:10