c# - Redirection after loggin not working -
i have problem redirecton after seccesful login. basicly wnat when user enters username , login, query search in table, , retrieves adequate role, , based role, redirects user specific folder. trying code
private void imgbtnlogin_click(object sender, imageclickeventargs e) { formsauthentication.initialize(); sqlconnection con = new sqlconnection("data source=.; initial catalog = axastock; integrated security = true"); sqlcommand cmd = con.createcommand(); cmd.commandtext = "select r.nomrole collaborateur c, role r r.idrole=c.idrole , matricule=@matricule , password=@password"; cmd.parameters.add("@matricule", sqldbtype.varchar, 64).value = txtlogin.text; cmd.parameters.add("@password", sqldbtype.varchar, 128).value = txtpassword.text; con.open(); sqldatareader reader = cmd.executereader(); if (reader.read()) { formsauthenticationticket ticket = new formsauthenticationticket(1, txtlogin.text, 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["returnurl"]; if (returnurl == null) { returnurl = "/"; } } else { lblerror.text = "matricule / mot de passe incorrect. réssayez !"; lblerror.visible = true; } reader.close(); con.close(); } my web.config
<system.web> <authentication mode="forms"> <forms name=".aspxformsauth" loginurl="login.aspx" protection="all" path="/"/> </authentication> <authorization> <allow users="*"/> </authorization> </system.web> <location path="tp"> <system.web> <authorization> <allow roles="technicien de proximité"/> <deny users="*"/> </authorization> </system.web> </location> <location path="cdp"> <system.web> <authorization> <allow roles="chef de projet"/> <deny users="*"/> </authorization> </system.web> </location> when click button login, doesnt redirect me specific folder, stays in login page without giving me message. how can redirected tp/accueil.aspx ?? please help
you not redirecting anywhere in code. code authenticate user. store info cookies. seek
if (reader.read()) { formsauthenticationticket ticket = new formsauthenticationticket(1, txtlogin.text, 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["returnurl"]; formsauthentication.redirectfromloginpage(txtlogin.text, false); if (returnurl == null) { returnurl = "/"; } } also in web.config. add together <system.web>
<authentication mode="forms"> <forms loginurl="login.aspx" name="logck"> </forms> </authentication> from msdn
in url http://www.contoso.com/login.aspx?returnurl=caller.aspx, redirectfromloginpage method redirects tothe homecoming url caller.aspx. if returnurl variable not exist, redirectfromloginpage method redirects url in defaulturl property. set desired url homecoming url parameter. more info read redirectfromloginpage.
c# asp.net
No comments:
Post a Comment