客户端上的已登录用户显示到其他页面

本文关键字:其他 显示 用户 登录 客户端 | 更新日期: 2023-09-27 18:19:58

我有三个页面:Login.aspx、Index.aspx和一个名为GlobalData.cs 的C#类文件

Login.aspx背后的代码,用于从谷歌获取用户信息并显示在Index.aspx 上

这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.RelyingParty;
using OpenIdTest;
using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
using System.Web.Security;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;  
public partial class Account_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FuncOpenID();
    }
    protected void FuncOpenID()
    {
        OpenIdRelyingParty OIDRP = new OpenIdRelyingParty();
        var response = OIDRP.GetResponse();
        if (response != null)
        {
            switch (response.Status)
            {
                case AuthenticationStatus.Authenticated:
                    var fetchResponse = response.GetExtension<FetchResponse>();
                    Session["GoogleIdentifier"] = fetchResponse;
                    var Testresponse = Session["GoogleIdentifier"] as FetchResponse;
                    GlobalData.Email = Testresponse.GetAttributeValue(WellKnownAttributes.Contact.Email) ;
                    GlobalData.Name = Testresponse.GetAttributeValue(WellKnownAttributes.Name.First) ;
                    GlobalData.LastName = Testresponse.GetAttributeValue(WellKnownAttributes.Name.Last); 
                    FormsAuthentication.RedirectFromLoginPage(GlobalData.Email, false);   //(response.ClaimedIdentifier, false);
                    FormsAuthentication.RedirectFromLoginPage(GlobalData.Name, false);
                    FormsAuthentication.RedirectFromLoginPage(GlobalData.LastName, false); 
                    break;
                case AuthenticationStatus.Canceled:
                    break;
                case AuthenticationStatus.Failed:
                    break;
            }
        }
    }

    protected void OpenLogin_Click(object src, CommandEventArgs e)
    {
        string StrUri = e.CommandArgument.ToString();
        OpenIdRelyingParty openid = new OpenIdRelyingParty();
        var b = new UriBuilder(Request.Url) { Query = "" };
        var req = openid.CreateRequest(StrUri);
        var fetchRequest = new FetchRequest();
        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.First);
        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.Last);
        req.AddExtension(fetchRequest);
        req.RedirectToProvider();
    }
    protected void btnLoginToGoogle_Click(object sender, EventArgs e)
    {
    }
}

更新类文件后面的代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    namespace OpenIdTest
    {
        public class GlobalData
        {
            public  string Email = "";
            public  string Name = "";
            public  string LastName = "";
            public  string test = "";
            public  string FullName = "";
        }

Index.aspx背后的代码如下:

namespace OpenIdTest
{
public partial class Rights : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["U_EMAIL"] = GlobalData.Email;
        Session["U_NAME"] = GlobalData.Name;
        Session["U_LASTNAME"] = GlobalData.LastName;   

        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|OID.mdb;Persist Security Info=False;");
        con.Open(); 
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandText = "Select * from EMAILS WHERE FLAG='Allowed' and  EMAIL= '" + GlobalData.Email + "'";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        foreach (DataRow row in ds.Tables[0].Rows)
        {
          String email = row["EMAIL"].ToString();
          if (email == null)
          {
              Response.Redirect("Login.aspx");
          }
          else
          {
              Label2.Text = Session["U_EMAIL"].ToString();
              Label1.Text = Session["U_NAME"].ToString();
              Label3.Text = Session["U_LASTNAME"].ToString();
              Label1.Visible = false;
              Label3.Visible = false;
              Label4.Text = Label1.Text + ' ' + Label3.Text;
          }
        }
        con.Close(); 
    }

}
}

现在我正在写我面临的严肃问题。我的网页使用openid的机制是,当用户单击Login.aspx上的"登录"按钮时,用户在从谷歌进行身份验证后会返回到谷歌邮件。在Index.aspx上,我已经从My自己的数据库中重新验证了用户,如果谷歌返回的电子邮件存在于我的数据库中,则用户应该查看该页面,如果电子邮件不存在于DB然后用户重定向到登录.aspx.Ok现在的问题是,当我从谷歌和我自己的数据库进行身份验证后登录Index.aspx时,Index.aspx会在Index.aspx.aspx上显示准确的信息,如电子邮件全名等。这意味着,如果我从任何其他浏览器、PC或会话登录Index..aspx,那么用户也会成功登录Index.asp。当我刷新第一次登录时Index.aspx,然后在CurrentIndex.aspx.aspx页面上显示第二个登录的用户信息。这意味着当多个用户试图登录到Index.aspx时,每次刷新Index.aspx上的时,都会显示最后一个用户登录到Index.aspx上的信息。有人能告诉我我到底缺少了什么吗?这就是为什么显示这个问题。我已经将所有页面的所有代码都放进去了,请帮助我在代码中添加或从代码中删除

客户端上的已登录用户显示到其他页面

存储在用户级别的信息永远不能存储在静态中。静态属性本质上意味着只有一个用户会登录到你的网站,它将是最后一个登录的人的信息。更改代码以在会话中存储这些值,然后这个问题就会消失。

编辑:很难说发生了什么,但这看起来仍然是静态的,所以你需要替换它:

GlobalData.Email = Testresponse.GetAttributeValue(WellKnownAttributes.Contact.Email) ;
GlobalData.Name = Testresponse.GetAttributeValue(WellKnownAttributes.Name.First) ;
GlobalData.LastName = Testresponse.GetAttributeValue(WellKnownAttributes.Name.Last); 

有了这个:

Session["U_Email"] = Testresponse.GetAttributeValue(WellKnownAttributes.Contact.Email) ;
Session["U_Name"] = Testresponse.GetAttributeValue(WellKnownAttributes.Name.First) ;
Session["U_LastName"] = Testresponse.GetAttributeValue(WellKnownAttributes.Name.Last); 

因为同样,您不能使用GlobalData,因为它是静态的。