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

谢谢。这工作 –