Försöker få ett inloggnings-script att fungera men det vill sig inte riktigt.FormsIdentity id = (FormsIdentity)User.Identity; Ger följande error: System.Inva
Följande rad:
FormsIdentity id = (FormsIdentity)User.Identity;
ger följande errormeddelande:
Unable to cast object of type 'System.Security.Principal.GenericIdentity' to type 'System.Web.Security.FormsIdentity'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Security.Principal.GenericIdentity' to type 'System.Web.Security.FormsIdentity'.
<code>
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
<script language="C#" runat="server">
void OnSubmit( Object sender, EventArgs e )
{
HttpCookie cookie = FormsAuthentication.GetAuthCookie (UserName.Text, RememberMe.Checked);
FormsIdentity id = (FormsIdentity)User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
if( CustomAuthenticate( UserName.Text, Password.Text ) )
{
cookie.Expires = DateTime.Now + new TimeSpan(7, 0, 0, 0);
Response.Cookies.Add( cookie );
Response.Redirect( FormsAuthentication.GetRedirectUrl( UserName.Text, RememberMe.Checked ) );
}
else
{
Output.Text = "Invalid login";
}
}
bool CustomAuthenticate (string username, string password)
{
string connectionString = @"Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=forum;uid=root;pwd=hemligt;";
OdbcConnection conn= new OdbcConnection(connectionString);
conn.Open();
OdbcDataAdapter da = new OdbcDataAdapter
("select password from users " +
"where username = \'" + username + "\'", conn);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable table = ds.Tables[0];
conn.Close();
foreach (DataRow row in table.Rows) {
string pw = row[0].ToString ().TrimEnd (new char[] { ' ' });
if (String.Compare (password, pw, false) == 0)
return true;
}
return false;
}
</script>
</code>