【问题标题】:Use of Location tag in authorization在授权中使用位置标签
【发布时间】: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


【解决方案1】:

这种情况可能不是真的:

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...。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 2016-09-24
    • 2016-06-14
    相关资源
    最近更新 更多