Skip to main content
 首页 » 编程设计

c#之需要认证的页面上的 IsPostBack

2024年02月13日11jirigala

我有一个带有以下代码的 asp.net 代码隐藏文件:

    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!IsPostBack) 
        { 
            if (User.Identity.IsAuthenticated) 
            { 

后面的一段代码用于渲染一些控件,应该只对经过身份验证的用户可见;我正在使用 asp.net Identity。

我的问题是:我应该保留我的代码,还是像这样重写它会更安全:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (User.Identity.IsAuthenticated) 
    { 
        if (!IsPostBack) 
        { 

请您参考如下方法:

Simply best in second way , Because if the user does authenticate, then only hit next code.

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (User.Identity.IsAuthenticated) 
    { 
        if (!IsPostBack) 
        { 

我写过这段代码,像asp.net页面生命周期

S 伊尔弗

  • 初始化

  • L-负载

  • V-验证

  • 电子事件

  • R 渲染

所以代码看起来更好的方法是

protected void Page_Load(object sender, EventArgs e)//Load 
    { 
        if (User.Identity.IsAuthenticated)//Validation 
        { 
            if (!IsPostBack) 
            { 
              //Rendering 
              . 
              . 
              . 
              .