2013-10-01 47 views
0

我有一个web.config文件通过我试图提供基于角色的安全性我的应用程序使用位置标签。我已经看了很多文章,并移动到结果,位置标签提供了访问限制的文件夹授权的用户就像在我的web配置我有一个文件夹“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); 
      } 

     } 
     } 
    } 

任何帮助????

+0

您正在混合身份验证和授权。看看微软的基于声明的授权框架,它应该为你解决问题。 –

回答

0

可能的这种情况下是不正确的:

if (Request.QueryString["ReturnUrl"] == null) 

即使是这样,你不注销用户或设置自己的角色为“HR” ......等一个良好的重定向,他们将无法访问该页面。

您可以登录与FormsAuthentication.SetAuthCookie用户。

if (TextBox1.Text.Trim() == "ABC" && TextBox2.Text.Trim() == "Gupta" && TextBox3.Text == "HR") 
{ 
    FormsAuthentication.SetAuthCookie("ABC", true); //this will login the user as ABC 

    String returnUrl1; 
    //I don't understand how the logic below determines successful login or not. 

    // the login is successful 
    if (Request.QueryString["ReturnUrl"] == null) 
    { 
     returnUrl1 = "HRPages/WelcomeHR.aspx"; 
    } 
    //login not unsuccessful 
    ... 

您还必须使用Role Management ...你不引用。