我需要根据角度设置权限。
部分web.config如下:<?xml version="1.0"?>
<configuration>
<connectionStrings>
  <add name="TESTConnectionString3" connectionString="Data Source=192.168.10.254;Initial Catalog=TEST;User ID=sa;Password=123456"
   providerName="System.Data.SqlClient" />
  <add name="TESTConnectionString" connectionString="Data Source=192.168.10.254;Initial Catalog=TEST;User ID=sa;Password=123456"
   providerName="System.Data.SqlClient" />
 </connectionStrings>
<system.web>
<authentication mode="Forms">
<forms name=".ASPXAUTH" loginUrl="Login.aspx" defaultUrl="MainPage.aspx" protection="All" timeout="30" path="/"></forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>

<machineKey decryption="Auto" validation="SHA1"/>
<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"/>
</assemblies>
</compilation>
<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 verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
</system.web>
<location path="MainPage.aspx">
<system.web>
<authorization>
<allow roles="admin,provider,purchaser"/>
<deny users="*"></deny>
</authorization>
</system.web>
</location>
<location path="extended_name.xml">
<system.web>
<authorization>
<deny users="*"></deny>
</authorization>
</system.web>
</location>后台程序如下:protected void btn_dl_Click(object sender, EventArgs e)
    {
        string login = txt_user.Text;
        string passwod = txt_paswd.Text;
        if (IsInUser(login, passwod) == true)
        {
            string role = GetRole(login);
            if (role != null)
            {
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, login, DateTime.Now, DateTime.Now.AddMinutes(30), false, role, "/");
                string hashticket = FormsAuthentication.Encrypt(ticket);
                HttpCookie usercookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashticket);
                Context.Response.Cookies.Add(usercookie);
                if (Context.Request["ReturnUrl"] != null)
                {
                    Response.Redirect(Context.Request["ReturnUrl"]);
                }
                else
                {
                    Response.Redirect(FormsAuthentication.DefaultUrl);
                }
            }
        }
        else
            Response.Write("<script>alert('用户名或密码不正确!');document.location=document.location;</script>");
    }    public bool IsInUser(string login, string passwod)
    {
        string comm = "select userID,login,password,name,company,companyID,power from [user] where login='" + login + "' and password='" + passwod + "'";
        string conn = "server=192.168.10.254;database=TEST;uid=sa;pwd=19780411";
        try
        {
            SqlConnection thisConntection = new SqlConnection(conn);
            thisConntection.Open();
            SqlCommand thisCommand = thisConntection.CreateCommand();
            thisCommand.CommandText = comm;
            SqlDataReader thisReader = thisCommand.ExecuteReader();
            if (thisReader.Read())
            {
                Response.Write("<script>window.location.href('MainPage.aspx')</script>");
                //Session["userID"] = thisReader[0];
                Session["login"] = thisReader[1];
                Session["name"] = thisReader[3];
                Session["company"] = thisReader[4];
                Session["companyID"] = thisReader[5];
                Session["power"] = thisReader[6];
                thisReader.Close();
                thisConntection.Close();
                return true;
            }
            else
            {
                thisReader.Close();
                thisConntection.Close();
                return false;
            }
        }
        catch
        {
            Response.Write("<script>alert('异常错误!');document.location=document.location;</script>");
            return false;
        }
    }    public string GetRole(string login)
    {
        string comm = "select login,role from [user] where login='" + login + "'";
        string conn = "server=localhost;database=TEST;uid=sa;pwd=111";
        try
        {
            SqlConnection thisConntection = new SqlConnection(conn);
            thisConntection.Open();
            SqlCommand thisCommand = thisConntection.CreateCommand();
            thisCommand.CommandText = comm;
            SqlDataReader thisReader = thisCommand.ExecuteReader();
            if (thisReader.Read())
            {
                Response.Write("<script>window.location.href('MainPage.aspx')</script>");
                string role = thisReader[1].ToString().Trim();
                thisReader.Close();
                thisConntection.Close();
                return role;
            }
            else
            {
                thisReader.Close();
                thisConntection.Close();
                return null;
            }
        }
        catch
        {
            Response.Write("<script>alert('异常错误!');document.location=document.location;</script>");
            return null;
        }
    }
然后,Global.asax中在添加protected 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信息
        }
    }现在的问题是,我在登录界面输入用户名和密码后,又跳到登录界面了,不管输入哪个用户名密码都是这样,是不是还要在web.config中加入什么元素,请高手指教,急~~~~!

解决方案 »

  1.   

    配置OK,可能是用户不是这些角色:admin,provider,purchaser,所以跟转回去了
    设几个断点,跟一下
      

  2.   

    http://msdn.microsoft.com/zh-cn/library/aa480475.aspx
      

  3.   

        <location path="MainPage.aspx">
            <system.web>
                <authorization>
                    <allow roles="admin,provider,purchaser"/>
                    <deny users="*"></deny>
                </authorization>
            </system.web>
        </location>
        <location path="extended_name.xml">
            <system.web>
                <authorization>
                    <deny users="*"></deny>
                </authorization>
            </system.web>
        </location>
    如果没记错的话,这是拒绝所有人的意思吧?!我正在使用《Csdn收音机》第一时间获取最新动态!
      

  4.   

    <deny user=“*"></deny>
    应该是:<deny user="?"></deny>吧?我正在使用《Csdn收音机》第一时间获取最新动态!
      

  5.   

    7楼,我在网上查了资料
    <allow roles="admin,provider,purchaser"/>
      <deny users="*"></deny>
    这样是可以的,意思是拒绝所有用户,但允许admin,provider,purchaser三种角色的用户访问。
      

  6.   

    如果改成<deny user="?"></deny>的话,那么,只要是登录了的用户,不管哪个文件都可以访问了,这是不允许的。。
      

  7.   

    SSO。?
    FORM认证
      

  8.   

    location path="允许匿名访问的文件夹名称或者文件路径"   
    <configuration>   
    <system.web>   
    <authentication mode="Forms">   
    <forms name="MYWEBAPP.ASPXAUTH" loginUrl="login.aspx"   
    protection="All" path="/"/>   
    </authentication>   
    <authorization>   
    <allow users="*"/>   
    </authorization>   
    </system.web>   
    <location path="admin">   
    <system.web>   
    <authorization>   
    <allow roles="Administrator"/>   
    <deny users="*"/>   
    </authorization>   
    </system.web>   
    </location>   
    <location path="users">   
    <system.web>   
    <authorization>   
    <allow roles="User"/>   
    <deny users="*"/>   
    </authorization>   
    </system.web>   
    </location>   
    </configuration>   
    if (User.Identity.IsAuthenticated&&User.IsInRole("user"))   
      {   
      Response.Redirect("index.aspx");   
      }   
      

  9.   

    12楼大哥,能不能说得清楚一点。
    if (User.Identity.IsAuthenticated&&User.IsInRole("user"))   
      {   
      Response.Redirect("index.aspx");   
      }这段代码放在哪?