私は同様の質問を検索しましたが、問題を解決できませんでした。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です。私は設定ページのフォームとして認証を設定しました。 助けがあれば助かります。
感謝。私は 'FormsAuthenticationTicket'を追加しました。私はまだ同じ結果を得ています。私はどのようにチェックしているかを示すために質問を編集しました。また、私はどのようにパスワードを保存する必要がありますか私はそれを暗号化すべきですか? – RPK
パスワードは常にSaltで暗号化してください。また、すべてのnessecary 'authentication'アイテムを[Web.Config](https://msdn.microsoft.com/en-us/library/1d3t3c61(v = vs.100).aspx)に追加しましたか? – VDWWD
私はconfgファイルを表示するために編集した質問をチェックしてください。また、 'FormsAuthentication.RedirectFromLoginPage(userName、true);'は動作しません。 – RPK