2017-04-12 94 views
1

我想從一個asp.net登錄控件傳遞一個值到一個完全不同的site.master頁面。這是我的login.aspx.cs頁 -會話在site.master頁面返回空值

protected void LoginUser_OnAuthenticate(object sender, AuthenticateEventArgs e) 
    { 
     Session["username"] = LoginUser.UserName; 
     Security.AuthenticateUser(LoginUser.UserName, LoginUser.Password, LoginUser.RememberMeSet); 
    } 

的這部分代碼從login.aspx的頁面收到值 -

<asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false" OnAuthenticate="LoginUser_OnAuthenticate"> 
    <div class="form-group"> 
     <label>Username</label> 
     <asp:TextBox ID="UserName" runat="server" AutoCompleteType="None" CssClass="textEntry ltr-dir"></asp:TextBox> 
    </div> 
    <div class="form-group"> 
     <label>Password</label> 
     <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry ltr-dir" TextMode="Password"></asp:TextBox> 
    </div> 
</asp:Login> 

這是我的Site.Master頁 -

var username = Session["username"].ToString(); 

var settings = ConfigurationManager.ConnectionStrings["BlogEngine"].ConnectionString; 
SqlConnection conn = new SqlConnection(settings); 

無論何時我調試,var username都會得到一個空值。而在login.aspx.cs頁面中,它將用戶名的值傳遞到會話中。

請問我該如何解決?

NB:Security.AuthenticateUer()方法 -

public static bool AuthenticateUser(string username, string password, bool rememberMe) 
    { 
     string un = (username ?? string.Empty).Trim(); 
     //string pw = (password ?? string.Empty).Trim(); 

     if (!string.IsNullOrWhiteSpace(un)) 
     { 
      var user = Membership.GetUser(un); 
      string res = Convert.ToString(user); 
      bool isValidated = Membership.ValidateUser(res, DEFAULT_PASSWORD); 
      if (isValidated) 
      { 
       if (BlogConfig.SingleSignOn) 
       { 
        FormsAuthentication.SetAuthCookie(un, rememberMe); 
        return true; 
       } 

       HttpContext context = HttpContext.Current; 
       DateTime expirationDate = DateTime.Now.Add(FormsAuthentication.Timeout); 

       FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
        1, 
        un, 
        DateTime.Now, 
        expirationDate, 
        rememberMe, 
        $"{SecurityValidationKey}{AUTH_TKT_USERDATA_DELIMITER}{Blog.CurrentInstance.Id}", 
        FormsAuthentication.FormsCookiePath 
       ); 

       string encryptedTicket = FormsAuthentication.Encrypt(ticket); 

       // setting a custom cookie name based on the current blog instance. 
       // if !rememberMe, set expires to DateTime.MinValue which makes the 
       // cookie a browser-session cookie expiring when the browser is closed. 
       System.Web.HttpCookie cookie = new System.Web.HttpCookie(FormsAuthCookieName, encryptedTicket); 
       cookie.Expires = rememberMe ? expirationDate : DateTime.MinValue; 
       cookie.HttpOnly = true; 
       context.Response.Cookies.Set(cookie); 

       string returnUrl = context.Request.QueryString["returnUrl"]; 
       Console.WriteLine("Redirect To This URL :" + returnUrl); 

       // ignore Return URLs not beginning with a forward slash, such as remote sites. 
       if (string.IsNullOrWhiteSpace(returnUrl) || !returnUrl.StartsWith("/")) 
        returnUrl = null; 

       if (!string.IsNullOrWhiteSpace(returnUrl)) 
       { 
        context.Response.Redirect(returnUrl); 
       } 
       else 
       { 
        if (IsReportUser(un)) 
        { 
         var reportPage = ""; 
         context.Response.Redirect(reportPage); 
        }; 

        context.Response.Redirect(Utils.RelativeWebRoot); 
       } 

       return true; 
      } 
     } 
     return false; 
    } 
+0

login.aspx是否使用相同的母版頁?如果是這樣的話,'session [「username」]'將在null登錄'login.aspx'。 – Win

+0

@Win,login.aspx使用不同的母版頁 - account.master。一旦login.aspx.cs認證用戶,它將用戶登錄到site.master。 –

+0

你能顯示Security.AuthenticateUser的代碼嗎? – Win

回答

1

由於您使用FormAuthentication,用戶名真正的內部存儲原理對象。您可以檢索像這樣的用戶名 -

var username = User.Identity.Name; 
+0

是這個System.Web的命名空間嗎?因爲我不斷收到名稱'Üser'在當前上下文中不存在,並且我導入了System.Web –

+1

好吧,我使用了var username = HttpContext.Current.User.Identity.Name。讓我測試一下並回復你。 –

+0

謝謝。這工作 –