2016-11-25 55 views
0

私は同様の質問を検索しましたが、問題を解決できませんでした。User.Identity.IsAuthenticatedは常にfalseです。e.Authenticated = true

私はこのようにチェックしています

<asp:Login ID="Login1" runat="server" Width="247px"  OnAuthenticate="Login1_Authenticate1"> 
       </asp:Login> 

C#コード

public partial class login : System.Web.UI.Page 
{ 
private SqlConnection con = new  SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); 

protected void Page_Load(object sender, EventArgs e) 
{ 

} 
protected void Login1_Authenticate1(object sender, AuthenticateEventArgs e) 
{ 
    string userName = Login1.UserName; 
    string password = Login1.Password; 

    bool result = UserLogin(userName, password); 
    if ((result)) 
    { 
     e.Authenticated = true; 
     FormsAuthentication.SetAuthCookie(userName, true); 
     Response.Redirect("http://localhost:57000/Default"); 
    } 
    else 
    { 
     e.Authenticated = false; 
    } 
} 
private bool UserLogin(string userName, string password) 
{ 

    //' declare the command that will be used to execute the select statement 
    SqlCommand com = new SqlCommand("SELECT Employee_Email FROM Employee_Detail WHERE Employee_Email = @UserName AND Password = @Password", con); 

     // set the username and password parameters 
     com.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = userName; 
     com.Parameters.Add("@Password", SqlDbType.NVarChar).Value = password; 

     con.Open(); 
     //' execute the select statment 
     string result = Convert.ToString(com.ExecuteScalar()); 
     //' check the result 
     if (string.IsNullOrEmpty(result)) 
     { 
      //invalid user/password , return flase 
      return false; 
     } 
     else 
     { 
      // valid login 
      return true; 
     } 
    } 

} htmlコード。 if (User.Identity.IsAuthenticated) { Page.Title = "Home page for " + User.Identity.Name; } else { Page.Title = "Home page for guest user."; } 設定ファイル

<authentication mode="Forms"> <forms defaultUrl="~/Default.aspx" loginUrl="~/login.aspx" name="__Auth" slidingExpiration="true" timeout="2880"></forms> </authentication>

ログインが正常に動作しますが、次のページでUser.Identity.IsAuthenticatedをチェックするとき、それは常にfalseです。私は設定ページのフォームとして認証を設定しました。 助けがあれば助かります。

答えて

0

またFormsAuthenticationTicket

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, userName, DateTime.Now, DateTime.Now.AddDays(30), true, String.Empty); 
string encryptedTicket = FormsAuthentication.Encrypt(ticket); 
HttpCookie authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); 
authenticationCookie.Expires = ticket.Expiration; 
Response.Cookies.Add(authenticationCookie); 

FormsAuthentication.SetAuthCookie(userName, true); 

を設定する必要があり、それはあなたが、プレーンテキストのパスワードを格納しているように見えます代わりにResponse.Redirect

FormsAuthentication.RedirectFromLoginPage(userName, true); 

RedirectFromLoginPageを使用することをお勧めします。それをしないでください。応答のための

UPDATE

<sessionState mode="InProc" cookieless="false" timeout="1440" /> 
<authentication mode="Forms"> 
    <forms cookieless="UseCookies" timeout="43200" defaultUrl="~/Default.aspx" loginUrl="~/login.aspx" /> 
</authentication> 
+0

感謝。私は 'FormsAuthenticationTicket'を追加しました。私はまだ同じ結果を得ています。私はどのようにチェックしているかを示すために質問を編集しました。また、私はどのようにパスワードを保存する必要がありますか私はそれを暗号化すべきですか? – RPK

+0

パスワードは常にSaltで暗号化してください。また、すべてのnessecary 'authentication'アイテムを[Web.Config](https://msdn.microsoft.com/en-us/library/1d3t3c61(v = vs.100).aspx)に追加しましたか? – VDWWD

+0

私はconfgファイルを表示するために編集した質問をチェックしてください。また、 'FormsAuthentication.RedirectFromLoginPage(userName、true);'は動作しません。 – RPK

関連する問題