2017-04-12 2 views
1

asp.netログインコントロールから全く異なるsite.masterページに値を渡そうとしています。これは私のlogin.aspx.csページです -セッションがsite.masterページでnullを返す

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のユーザー名がnull値を取得しています。 login.aspx.csページでは、ユーザー名の値がセッションに渡されます。

どうすれば解決できますか?

NB:Security.AuthenticateUer()方法 - あなたはFormAuthenticationを使用しているので

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 "]'は 'login.aspx'にログインしている間はnullになります。 – Win

+0

@Win、login.aspxは別のマスターページ - account.masterを使用します。 login.aspx.csがユーザーを認証すると、ユーザーはsite.masterにログインします。 –

+0

Security.AuthenticateUserのコードを表示できますか? – Win

答えて

1

、ユーザ名が実際の原理オブジェクト内に格納されます。あなたはこのようなユーザー名を取得できます -

var username = User.Identity.Name; 
+0

はこのSystem.Webのネームスペースですか?私は名前を取得し続けているので、現在のコンテキストでは 'Üser'は存在しません。私はSystem.Webをインポートしました。 –

+1

OK、var username = HttpContext.Current.User.Identity.Nameを使用しました。これを試してみましょう。 –

+0

ありがとうございます。これは動作します –

関連する問題