2012-01-10 104 views
0

我有三個頁面:爲Login.aspx,Index.aspx的和C#類文件名爲GlobalData.cs登錄的用戶在客戶端顯示到其他網頁

中的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(); 
    } 



} 
} 

現在,我寫我的嚴重的問題,對此我facing.My機制我的網頁上使用OpenID的是,當用戶點擊登錄按鈕Login.aspx它需要用戶谷歌郵件認證後,從谷歌用戶回到Index.aspx.And這裏INdex.aspx我已經從mY自己的數據庫重新認證用戶,如果谷歌已經返回的電子郵件存在在我的數據庫然後用戶應該查看頁面,如果電子郵件dnt存在數據庫然後用戶重定向到Login.aspx.Ok現在的問題是,當我登錄到Index.aspx從谷歌和我自己的數據庫認證後,我ndex.aspx顯示準確的信息,如電子郵件全名等在Index.aspx.and意思是,如果我從任何其他瀏覽器或PC或會話登錄到Index.aspx,然後也用戶登錄到Index.aspx Succesfuly.And當我刷新第一個記錄索引.aspx然後它顯示當前Index.aspx page上的第二個登錄用戶信息。它表示當多個用戶嘗試登錄到Index.aspx時,然後在每次刷新時Index.aspx將上次用戶登錄的信息顯示爲Index。 aspx on Index.aspx.Can任何人都可以告訴我我究竟是什麼想念這就是爲什麼這個問題顯示。我把所有頁面的所有代碼請幫助我必須添加代碼或從代碼中刪除

+0

爲什麼你首先使用靜態字段?在開發Web應用程序時(無論基礎技術如何),與用戶相關的所有內容都應存儲在各自的用戶會話中,以防止這裏描述的醜陋的錯誤。靜態字段的缺點是,當Web服務器決定啓動一個新進程而不是重新使用舊進程時(例如關閉舊進程之後不會被訪問)時,可能會刪除。 – Nuffin 2012-01-10 13:28:29

+0

爲什麼你不看一點點到ASP.NET會話變量和嘗試自己?如果將來出現更多問題,我很樂意提供幫助,但這並不意味着我會爲您做好工作(並且您必須以任何方式學習,如果您想繼續編寫ASP.NET需要能夠同時進行多用戶訪問的應用程序)。 – Nuffin 2012-01-10 13:52:58

+0

會話變量只有在會話開始時設置它們時纔有用,換句話說:一旦用戶登錄。由於您在從之前使用的相同全局變量實際使用它們之前初始化它們,因此沒有任何更改。 – Nuffin 2012-01-11 08:01:42

回答

2

存儲在一個用戶級別永遠不能存儲在一個靜態的。靜態屬性本質上意味着只有一個用戶會登錄到您的網站,並且它將成爲最後登錄人員的信息。更改您的代碼以將這些值存儲在會話中,然後此問題將消失。

編輯:這很難說,這是怎麼回事,但是這仍然看起來像一個靜態的,所以你需要替換此:

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。

+0

問題描述迫切需要編輯......看起來像他從谷歌翻譯並粘貼它,當您發佈解決方案時,我處於編輯的中間 – Rafael 2012-01-10 13:47:15

+0

任何ONe都可以告訴我何人以及在哪裏使用會話變量進行解析我的PRoblem – 2012-01-11 03:51:31

+0

AnyOne請檢查我的更新我已經刪除靜態從類和使用的會話變量來存儲用戶信息但情況是相同的 – 2012-01-11 04:47:44