【发布时间】:2013-10-01 21:51:13
【问题描述】:
我有一个 web.config 文件,我试图通过该文件使用 location 标签为我的应用程序提供基于角色的安全性。我已经阅读了许多文章并转向结果,即 location 标签为授权用户提供了对有限文件夹的访问权限,例如在我的网络配置中我有一个文件夹“HRpages " 仅允许访问具有“HR”角色的用户。但我不清楚如何在我的代码文件(Login.aspx.cs)中使用它进行授权。
注意:目前 “Login.aspx.cs” 不会将我重定向到“WelcomeHR.aspx”页面。不知道为什么。
Web.Config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="Login.aspx" defaultUrl="WelcomePage.aspx">
</forms>
</authentication>
<authorization>
<deny users="?" />
</authorization>
</system.web>
<location path="HRpages">
<system.web>
<authorization>
<allow roles="HR" />
<deny users="*" />
</authorization>
</system.web>
</location>
<location path="AdminPages">
<system.web>
<authorization>
<allow roles="Admin" />
<deny users="*" />
</authorization>
</system.web>
</location>
<system.webServer>
<defaultDocument>
<files>
<add value="AddTwoNumbers.asmx" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
Login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace WebServiceExample
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Trim() == "ABC" && TextBox2.Text.Trim() == "Gupta" && TextBox3.Text == "HR")
{
String returnUrl1;
// the login is successful
if (Request.QueryString["ReturnUrl"] == null)
{
returnUrl1 = "HRPages/WelcomeHR.aspx";
}
//login not unsuccessful
else
{
returnUrl1 = Request.QueryString["ReturnUrl"];
}
Response.Redirect(returnUrl1);
}
}
}
}
任何帮助????
【问题讨论】:
-
您正在混合身份验证和授权。查看 Microsoft 的基于声明的授权框架,它应该可以为您解决问题。
标签: c#-4.0 webforms authorization