web.config 文件对登陆用户授权

对于登陆的特定用户 可以在web.config 文件设置其权限

web.config 只给admin用户授权登陆

 <!--
            通过 <authentication> 节可以配置 ASP.NET 用来
            识别进入用户的
            安全身份验证模式。
        -->
    <authentication mode="Forms">
      <forms name="Login" loginUrl="login.aspx" defaultUrl="noteFlatroot/index.aspx" timeout ="60">
        <credentials passwordFormat="SHA1"></credentials>
      </forms>
     
    </authentication>
    <authorization >
      <deny users="?"/>
      <allow users ="admin"/>
      <deny users ="*"/>
     
    </authorization>

 

保存用户信息到验证票 验证权限的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

/// <summary>
///YanZLogin 的摘要说明
/// </summary>
public class YanZLogin
{
    public YanZLogin()
    { }
    //
    //TODO: 在此处添加构造函数逻辑
    //

    // 身份验证的代码      
    public static string AuthenticationTicket(string username)
    {
        FormsAuthenticationTicket tichet = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddHours(24), true, "");
        string hashticket = FormsAuthentication.Encrypt(tichet); //加密
        HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName);
        userCookie.Value = hashticket;//获取或设置单个 Cookie 值。
        userCookie.Expires = tichet.Expiration;//设置此Cookie 的过期日期和时间
        userCookie.Domain = FormsAuthentication.CookieDomain;//获取或设置将此 Cookie 与其关联的域
        HttpContext.Current.Response.Cookies.Add(userCookie);//将对象添加到Cookie 中去
        string requestUrl = FormsAuthentication.GetRedirectUrl(FormsAuthentication.FormsCookieName, false);
        //不要使用FormsAuthentication.RedirectFromLoginPage方法,因为这个方法会重写cookie 
        //重新定向到请求的url 

        return requestUrl;
    }

}

 

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.Configuration;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SerialNumber1.Create();
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        this.SerialNumber1.Create();
    }
    /// <summary>
    /// 提交登陆信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string returnUrl = Request.QueryString["ReturnUrl"];
        string Lpwd = ConfigurationManager.ConnectionStrings["pwd"].ToString();
        //判断验证码是否错误
        if (!SerialNumber1.CheckSN(txtYZ.Text.Trim()))
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('验证码错误!')</script>");
        }
        else
        {
            string name = txtName.Text;
            string pwd = txtPwd.Text;
          
           
            if (pwd.Equals(Lpwd))
            {
                YanZLogin.AuthenticationTicket(name);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>location.href='" + returnUrl + "'</script>");
            }
            else
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('密码错误!')</script>");
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值