查看: 798|回复: 13
|
为什么login不到的?ASP.net
[复制链接]
|
|
- login.aspx
- <%@ Page Language="C#" %>
- <%@ Import Namespace="System.Data" %>
- <%@ Import Namespace="System.Data.Odbc" %>
- <html>
- <head>
- <title>Login</title>
- </head>
- <script runat="server">
- private void btnLogin_Click(Object sender, EventArgs e)
- {
- FormsAuthentication.Initialize();
- string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=127.0.0.1;" + "DATABASE=aspnet;" + "UID=root;" + "PASSWORD=chen1983";
- OdbcConnection MyConnection = new OdbcConnection(MyConString);
- OdbcCommand cmd = MyConnection.CreateCommand();
- cmd.CommandText = "SELECT roles FROM users WHERE username=@Username.Text " + "AND password=@Password.Text";
- cmd.Parameters.Add("@username", OdbcType.VarChar, 64).Value = Username.Value;
- cmd.Parameters.Add("@password", OdbcType.VarChar, 128).Value = FormsAuthentication.HashPasswordForStoringInConfigFile(Password.Value, "md5"); // Or "sha1"
- MyConnection.Open();
- OdbcDataReader Reader = cmd.ExecuteReader();
- if (Reader.Read())
- {
- FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
- 1,
- Username.Value,
- DateTime.Now,
- DateTime.Now.AddMinutes(30),
- true,
- Reader.GetString(0),
- FormsAuthentication.FormsCookiePath);
- string hash = FormsAuthentication.Encrypt(ticket);
- HttpCookie cookie = new HttpCookie(
- FormsAuthentication.FormsCookieName,
- hash);
- if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
- Response.Cookies.Add(cookie);
- string returnUrl = Request.QueryString["welcome.aspx"];
- if (returnUrl == null) returnUrl = "/";
- Response.Redirect(returnUrl);
- }
- else
- {
- ErrorLabel.Text = "Username / password incorrect. Please try again.";
- ErrorLabel.Visible = true;
- }
- Reader.Close();
- MyConnection.Close();
- }
- </script>
- <body>
- <form runat="server">
- <p>Username: <input id="Username" runat="server" type="text"/><br />
- Password: <input id="Password" runat="server" type="password"/><br/>
- <asp:Button id="btnLogin" runat="server" OnClick="btnLogin_Click" Text="Login"/>
- <asp:Label id="ErrorLabel" runat="Server" ForeColor="Red" Visible="false"/></p>
- </form>
- </body>
- </html>
复制代码
- web.config
- <configuration>
- <appSettings/>
- <connectionStrings>
- <add name="ConnectionString" connectionString="Dsn=mydata;uid=root;pwd=chen1983"
- providerName="System.Data.Odbc" />
- </connectionStrings>
- <system.web>
- <compilation debug="true"/>
-
- <authentication mode="Forms">
- <forms name="MYWEBAPP.ASPXAUTH" loginUrl="login.aspx" protection="All" path="/"/>
- </authentication>
- <authorization>
- <allow users="*"/>
- </authorization>
- <globalization requestEncoding="UTF-8" responseEncoding="UTF-8"/>
- </system.web>
- <location path="administrators">
- <system.web>
- <authorization>
- <!-- Order and case are important below -->
- <allow roles="Administrator"/>
- <deny users="*"/>
- </authorization>
- </system.web>
- </location>
- <location path="users">
- <system.web>
- <authorization>
- <!-- Order and case are important below -->
- <allow roles="User"/>
- <deny users="*"/>
- </authorization>
- </system.web>
- </location>
- </configuration>
复制代码
- Global.asax
- <%@ Application Codebehind="Global.asax.cs" %>
- Global.asax.cs
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Web;
- using System.Web.SessionState;
- using System.Security.Principal
- namespace WebApplication1
- {
- public class Global : System.Web.HttpApplication
- {
- {
- protected void Application_AuthenticateRequest(Object sender,EventArgs e)
- {
- if(HttpContext.Current.User!=null)
- {
- if(HttpContext.Current.User.Identity.IsAuthenticated)
- {
- if(HttpContext.Current.User.Identity is FormsIdentity)
- {
- FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
- FormsAuthenticationTicket ticket = id.Ticket;
- string userData = ticket.UserData;
- string[] roles = userData.Split(',');
- HttpContext.Current.User = new GenericPrincipal(id,roles);
- }
- }
- }
- }
- }
- }
复制代码
- welcome.aspx
- <html>
- <head>
- <title>Welcome</title>
- <script runat="server">
- protected void Page_Load(Object sender, EventArgs e)
- {
- if (User.IsInRole("Administrator"))
- AdminLink.Visible = true;
- }
- </script>
- </head>
- <body>
- <form runat=server>
- <h2>Welcome</h2>
- <p>Welcome, anonymous user, to our web site.</p>
- <asp:HyperLink id="AdminLink" runat="server"
- Text="Administrators, click here." NavigateUrl="administrators/"/>
- </form>
- </body>
- </html>
复制代码 |
|
|
|
|
|
|
|
楼主 |
发表于 23-3-2006 01:19 PM
|
显示全部楼层
不知道为什么就是login不到。
一直说我的password/username错。
ErrorLabel.Text = "Username / password incorrect. Please try again.";
明明是对的呀。
system是可以跑的。
也没有error。 |
|
|
|
|
|
|
|
发表于 23-3-2006 05:08 PM
|
显示全部楼层
试试看用breakpoint trace回去看哪里出错。。 |
|
|
|
|
|
|
|
楼主 |
发表于 27-3-2006 09:12 AM
|
显示全部楼层
原帖由 红卜卜 于 23-3-2006 05:08 PM 发表
试试看用breakpoint trace回去看哪里出错。。
完全没有error的。
只是不知道为什么login不到?? |
|
|
|
|
|
|
|
楼主 |
发表于 29-3-2006 01:39 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 29-3-2006 09:20 PM
|
显示全部楼层
回复 #1 红发 的帖子
MySQL 我不熟
但应该不需要放 .Text 在SQL command 吧
@Password.Text
不知道是不是 |
|
|
|
|
|
|
|
楼主 |
发表于 30-3-2006 01:15 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 30-3-2006 05:28 PM
|
显示全部楼层
原帖由 红卜卜 于 23-3-2006 17:08 发表
试试看用breakpoint trace回去看哪里出错。。
红卜卜 也是搞网页编程的?。。。。 |
|
|
|
|
|
|
|
发表于 8-4-2006 11:27 AM
|
显示全部楼层
is it u use the visual studio and the web matrix to do this??
if can please post out ur error page to let other read and see whether can help u. |
|
|
|
|
|
|
|
楼主 |
发表于 8-4-2006 04:08 PM
|
显示全部楼层
原帖由 Freedom84 于 8-4-2006 11:27 AM 发表
is it u use the visual studio and the web matrix to do this??
if can please post out ur error page to let other read and see whether can help u.
我是从其他的网页看来的
http://www.codeproject.com/aspnet/formsroleauth.asp
run的时候就是没有error。
我也login不进我的pager。 |
|
|
|
|
|
|
|
发表于 8-4-2006 06:10 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 8-4-2006 07:58 PM
|
显示全部楼层
回复 #10 红发 的帖子
I read oredi the website u giv...
because u r using the ODBC, so i think is the connection problem...
do u hav register a new ODBC connection with the sql server at ur Data Source?? |
|
|
|
|
|
|
|
楼主 |
发表于 8-4-2006 10:26 PM
|
显示全部楼层
原帖由 Freedom84 于 8-4-2006 07:58 PM 发表
I read oredi the website u giv...
because u r using the ODBC, so i think is the connection problem...
do u hav register a new ODBC connection with the sql server at ur Data Source??
我希望你可以用中文因为这里是中文论坛会给他们扣分的。
我不是用SQLServer的。
我是用MySQL的。 |
|
|
|
|
|
|
|
楼主 |
发表于 8-4-2006 10:27 PM
|
显示全部楼层
原帖由 eddom 于 8-4-2006 06:10 PM 发表
你應該要用 Request 的方法吧
ASP.net不是不用这个的吗?
在同一页
[ 本帖最后由 红发 于 10-4-2006 12:22 PM 编辑 ] |
|
|
|
|
|
|
| |
本周最热论坛帖子
|