积极答复者
基于Forms角色验证不能访问角色控制页面

问题
-
我做了一个登录页面,采用了基于Forms的角色验证,登录及验证包括写入Cookies都正常,但发现配置了相关的角色信息后,用具有该角色的用户也不能访问配置页面。
---------------------------------------------------
目录结构:
根目录
根目录/Admin 后台管理目录
---------------------------------------------------
根目录的Web.Config主要代码:
<authentication mode="Forms"> <forms loginUrl="Admin/Login.aspx" name=".ASPXAUTH" timeout="30" path="/"></forms> </authentication>
Admin目录下的Web.Config主要代码:
<configuration> <system.web> <authorization> <deny users="?"/> </authorization> </system.web> <location path="DbManager.aspx"> <system.web> <authorization> <allow roles="Admins"/> <deny users="*"/> </authorization> </system.web> </location> <location path="CreateCode.aspx"> <system.web> <authorization> <allow users="?"/> </authorization> </system.web> </location> <location path="Login.aspx"> <system.web> <authorization> <allow users="?"/> </authorization> </system.web> </location>
if (AdminBll.ValidateLogin(myAdmin) == LoginResult.OK) { ClientScript.RegisterStartupScript(Page.GetType(), "", MessageBox.Alert("登陆成功")); string userRoles = "Admins"; //为测试方便直接填写的角色字符串 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, _UserName, DateTime.Now, DateTime.Now.AddMinutes(30), false, userRoles, "/"); string HaskTicket = FormsAuthentication.Encrypt(ticket); HttpCookie UserCookies = new HttpCookie(FormsAuthentication.FormsCookieName, HaskTicket); Response.Cookies.Add(UserCookies); if (string.IsNullOrEmpty(Request["ReturnUrl"])) { Response.Redirect("index.aspx"); } else { Response.Redirect(Request["ReturnUrl"]); } }
Global.asax中关键代码:
void Application_AuthorizeRequest(object sender, System.EventArgs e) { HttpApplication App = (HttpApplication)sender; HttpContext Ctx = App.Context; //获取本次Http请求相关的HttpContext对象 if (Ctx.Request.IsAuthenticated == true) //验证过的用户才进行role的处理 { FormsIdentity Id = (FormsIdentity)Ctx.User.Identity; FormsAuthenticationTicket Ticket = Id.Ticket; //取得身份验证票 string[] Roles = Ticket.UserData.Split(','); //将身份验证票中的role数据转成字符串数组 Ctx.User = new System.Security.Principal.GenericPrincipal(Id, Roles); //将原有的Identity加上角色信息新建一个GenericPrincipal表示当前用户,这样当前用户就拥有了role信息 } }
FormsIdentity Id = (FormsIdentity)User.Identity; FormsAuthenticationTicket Ticket = Id.Ticket; //取得身份验证票
- 已编辑 superfeeling 2010年3月16日 3:00
答案
-
你好,
我用Framework2.0做了个例子,也是可以的。
你可以到下面这个地方下载试试。
http://files.cnblogs.com/wuwei_chen/Framework2.0Test.rar
Microsoft Online Community Support- 已标记为答案 superfeeling 2010年3月21日 4:13
-
启用roleManager需要提供roleProvider。但是你这里的情况和roleManager的需要环境不一样。
你虽然使用了角色,但是和roleManager没有关系。
详细情况可以参考下面的文章。
http://msdn.microsoft.com/en-us/library/ms998314.aspx
Microsoft Online Community Support- 已标记为答案 肖小勇Moderator 2010年3月22日 1:08
全部回复
-
你好,
试试下面这种方式:
<?xml version="1.0"?> <!-- Note: As an alternative to hand editing this file you can use the web admin tool to configure settings for your application. Use the Website->Asp.Net Configuration option in Visual Studio. A full list of settings and comments can be found in machine.config.comments usually located in \Windows\Microsoft.Net\Framework\v2.x\Config --> <configuration> <appSettings/> <connectionStrings/> <location path="DbManager.aspx"> <system.web> <authorization> <allow roles="Admins"/> <deny users="?"/> </authorization> </system.web> </location> </configuration>
Microsoft Online Community Support -
不好意思,
的确是应该是下面这样:
<?xml version="1.0"?> <configuration> <appSettings/> <connectionStrings/> <location path="DbManager.aspx"> <system.web> <authorization> <allow roles="Admins"/> <deny users="*"/> </authorization> </system.web> </location> </configuration>
同时我估计你的根目录下的web.config中是不是有关于roleManager的默认配置,把它给注释掉。
我这边注释掉你的例子就可以运行了。
Microsoft Online Community Support -
郁闷了,就是不行。下面是我根目录的web.config的全部代码。
<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="ConnectionString" value="Server=(local);Database=News;User ID=sa;Password=lp_lucky;" /> <add key="FCKeditor:BasePath" value="~/fckeditor/" /> <add key="FCKeditor:UserFilesPath" value="/UploadFile/" /> <add key="OledbConnstr" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" /> <add key="DbPath" value="~/Data/db.mdb" /> </appSettings> <connectionStrings /> <system.web> <compilation debug="false" /> <authentication mode="Forms"> <forms loginUrl="Admin/Login.aspx" name=".ASPXAUTH" timeout="30" path="/"></forms> </authentication> </system.web> </configuration>
-
web.config是添加的默认的。我基本没怎么改,上面发的只是删掉了注释。.net版本是2.0,并且在IIS中配置了的。我是通过在项目上右击添加Web配置文件添加的web.config。
<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="ConnectionString" value="Server=(local);Database=News;User ID=sa;Password=lp_lucky;" /> <add key="FCKeditor:BasePath" value="~/fckeditor/" /> <add key="FCKeditor:UserFilesPath" value="/UploadFile/" /> <add key="OledbConnstr" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" /> <add key="DbPath" value="~/Data/db.mdb" /> </appSettings> <connectionStrings /> <system.web> <!-- 设置 compilation debug="true" 将调试符号插入 已编译的页面中。但由于这会 影响性能,因此只在开发过程中将此值 设置为 true。 --> <compilation debug="true" /> <!-- 通过 <authentication> 节可以配置 ASP.NET 使用的 安全身份验证模式, 以标识传入的用户。 --> <authentication mode="Forms"> <forms loginUrl="Admin/Login.aspx" name=".ASPXAUTH" timeout="30" path="/"></forms> </authentication> <!-- 如果在执行请求的过程中出现未处理的错误, 则通过 <customErrors> 节可以配置相应的处理步骤。具体说来, 开发人员通过该节可以配置 要显示的 html 错误页 以代替错误堆栈跟踪。 <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm" /> <error statusCode="404" redirect="FileNotFound.htm" /> </customErrors> --> </system.web> </configuration>
-
刚才试了一下,确实会自动生成。也可能是我记错了。但自动生成的web.config也还是这些内容,没有什么特殊的地方啊。
<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings/> <connectionStrings/> <system.web> <!-- 设置 compilation debug="true" 将调试符号插入 已编译的页面中。但由于这会 影响性能,因此只在开发过程中将此值 设置为 true。 --> <compilation debug="true" /> <!-- 通过 <authentication> 节可以配置 ASP.NET 使用的 安全身份验证模式, 以标识传入的用户。 --> <authentication mode="Windows" /> <!-- 如果在执行请求的过程中出现未处理的错误, 则通过 <customErrors> 节可以配置相应的处理步骤。具体说来, 开发人员通过该节可以配置 要显示的 html 错误页 以代替错误堆栈跟踪。 <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm" /> <error statusCode="404" redirect="FileNotFound.htm" /> </customErrors> --> </system.web> </configuration>
-
我的版本是Framework3.5的。
我的配置:
<?xml version="1.0"?> <!-- Note: As an alternative to hand editing this file you can use the web admin tool to configure settings for your application. Use the Website->Asp.Net Configuration option in Visual Studio. A full list of settings and comments can be found in machine.config.comments usually located in \Windows\Microsoft.Net\Framework\v2.x\Config --> <configuration> <configSections> <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/> <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/> <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/> <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/> <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/> </sectionGroup> </sectionGroup> </sectionGroup> </configSections> <system.web> <compilation debug="true"> <assemblies> <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <add assembly="System.Web.Extensions.Design, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <add assembly="Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> <add assembly="Microsoft.ReportViewer.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> <add assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/> <add assembly="CrystalDecisions.Shared, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/> <add assembly="CrystalDecisions.ReportSource, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/> <add assembly="CrystalDecisions.CrystalReports.Engine, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/> <add assembly="CrystalDecisions.ReportAppServer.ClientDoc, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/> <add assembly="CrystalDecisions.Enterprise.Framework, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/> <add assembly="CrystalDecisions.Enterprise.InfoStore, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/> </assemblies> <buildProviders> <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> </buildProviders> </compilation> <!-- The <authentication> section enables configuration of the security authentication mode used by ASP.NET to identify an incoming user. --> <authentication mode="Forms"> <forms name=".LoginCookie" cookieless="UseCookies" path="/" loginUrl="LoginDemo.aspx" protection="All" slidingExpiration="true"> </forms> </authentication> <!-- The <customErrors> section enables configuration of what to do if/when an unhandled error occurs during the execution of a request. Specifically, it enables developers to configure html error pages to be displayed in place of a error stack trace. --> <customErrors mode="Off" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm" /> <error statusCode="404" redirect="FileNotFound.htm" /> </customErrors> <pages> <controls> <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </controls> </pages> <httpHandlers> <remove path="*.asmx" verb="*"/> <add path="*.asmx" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/> <add path="*_AppService.axd" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/> <add path="ScriptResource.axd" verb="GET,HEAD" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/> <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false"/> <add verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/> </httpHandlers> <httpModules> <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <!--<add name="TestFileUpLoad" type="Test.CustomizeHttpModule"/>--> </httpModules> </system.web> <system.web.extensions> <scripting> <webServices> <authenticationService enabled="true" /> <roleService enabled="true" /> </webServices> </scripting> </system.web.extensions> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <providerOption name="CompilerVersion" value="v3.5"/> <providerOption name="WarnAsError" value="false"/> </compiler> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <providerOption name="CompilerVersion" value="v3.5"/> <providerOption name="OptionInfer" value="true"/> <providerOption name="WarnAsError" value="false"/> </compiler> </compilers> </system.codedom> <!-- The system.webServer section is required for running ASP.NET AJAX under Internet Information Services 7.0. It is not necessary for previous version of IIS. --> <system.webServer> <validation validateIntegratedModeConfiguration="false"/> <modules> <remove name="ScriptModule"/> <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </modules> <handlers> <remove name="WebServiceHandlerFactory-Integrated"/> <remove name="ScriptHandlerFactory"/> <remove name="ScriptHandlerFactoryAppServices"/> <remove name="ScriptResource"/> <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="CrystalImageHandler.aspx_GET" verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" preCondition="integratedMode"/> </handlers> </system.webServer> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/> <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/> <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
我的测试代码:
string userRoles = "Admin"; //为测试方便直接填写的角色字符串 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, "karl", DateTime.Now, DateTime.Now.AddMinutes(30), true, userRoles, "/"); string HaskTicket = FormsAuthentication.Encrypt(ticket); HttpCookie UserCookies = new HttpCookie(FormsAuthentication.FormsCookieName, HaskTicket); Response.Cookies.Add(UserCookies); Response.Redirect("~/FileUploadDemo/attachment.aspx");
global中的和你的一样,在Application_AuthorizeRequest方法中处理。
admin下的配置就是和我前面说的那样,就可以访问了。
Microsoft Online Community Support -
你好,
我用Framework2.0做了个例子,也是可以的。
你可以到下面这个地方下载试试。
http://files.cnblogs.com/wuwei_chen/Framework2.0Test.rar
Microsoft Online Community Support- 已标记为答案 superfeeling 2010年3月21日 4:13
-
启用roleManager需要提供roleProvider。但是你这里的情况和roleManager的需要环境不一样。
你虽然使用了角色,但是和roleManager没有关系。
详细情况可以参考下面的文章。
http://msdn.microsoft.com/en-us/library/ms998314.aspx
Microsoft Online Community Support- 已标记为答案 肖小勇Moderator 2010年3月22日 1:08