ASP.net role based authorization using froms authentication failshttp://forums.asp.net/t/1609661.aspx/1?ASP+net+role+based+authorization+using+froms+authentication+failsTue, 05 Oct 2010 13:30:32 -040016096614111926http://forums.asp.net/p/1609661/4111926.aspx/1?ASP+net+role+based+authorization+using+froms+authentication+failsASP.net role based authorization using froms authentication fails <p>Hi Dot Net Gurus,</p> <p>I am trying to implement a simple role based authorization using forms authentication in ASP.net. It works perfectly fine in my local system but fails when I deploy in production (shared hosting). Whenever I try to log in, rather than taking me to the default page in specified directory it throws me back to the login page. I suspect that there is some issues with the configuration but not sure where the problem is. The code is provided below:</p> <p><b>Web.config (root):</b> <pre class="prettyprint">&lt;authentication mode=&quot;Forms&quot;&gt; &lt;forms name=&quot;userId&quot; loginUrl=&quot;Login.aspx&quot; defaultUrl=&quot;Default.aspx&quot; path=&quot;/&quot; timeout=&quot;240&quot; requireSSL=&quot;false&quot; /&gt; &lt;/authentication&gt;</pre></p><p><b>Web.config (Member directory):</b></p> <pre class="prettyprint">&lt;configuration&gt; &lt;appSettings/&gt; &lt;connectionStrings/&gt; &lt;system.web&gt; &lt;authorization&gt; &lt;allow roles="Member" /&gt; &lt;deny users="*" /&gt; &lt;/authorization&gt; &lt;/system.web&gt; &lt;/configuration&gt;</pre></pre><p><b>Login.aspx:</b></p><p><pre class="prettyprint"> protected void btnLogin_Click(object sender, ImageClickEventArgs e) { String email = ""; String password = ""; try { email = txtEmail.Text; password = txtPassword.Text; Member member = null; member = _memberService.GetMemberByEmailAndPassword(email,password); if (member != null) { FormsAuthentication.Initialize(); String strRoles = "Member"; FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1, txtEmail.Text, DateTime.Now, DateTime.Now.AddMinutes(30), false, strRoles, FormsAuthentication.FormsCookiePath); Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(fat))); Response.Redirect("~/Member/Default.aspx"); } else { lblShowMessage.Text = "Login failed."; } } catch (Exception ex) { lblShowMessage.Text = "Login failed."; } } </pre><br> <b>Global.asax:</b></p><p><pre class="prettyprint">&lt;%@ Application Language="C#" %&gt; &lt;%@ Import Namespace="System.Web.Security" %&gt; &lt;%@ Import Namespace="System.Security.Principal" %&gt; protected void Application_AuthenticateRequest(object sender, EventArgs e) { //Fires upon attempting to authenticate the use if (!(HttpContext.Current.User == null)) { if (HttpContext.Current.User.Identity.IsAuthenticated) { if (HttpContext.Current.User.Identity.GetType() == typeof(FormsIdentity)) { FormsIdentity fi = (FormsIdentity)HttpContext.Current.User.Identity; FormsAuthenticationTicket fat = fi.Ticket; String[] astrRoles = fat.UserData.Split('|'); HttpContext.Current.User = new GenericPrincipal(fi, astrRoles); } } } } </pre></p> <p><br> Works fine in local machine but shared hosting is not taking the authenticated user to the pages inside the secured folders. What can be the issue? Any pointers would be welcome.</p> <p>-Das<br> </p> 2010-10-05T12:21:59-04:004112067http://forums.asp.net/p/1609661/4112067.aspx/1?Re+ASP+net+role+based+authorization+using+froms+authentication+failsRe: ASP.net role based authorization using froms authentication fails <p>Since you're using shared hosing, have you tried setting the applicationName attribute in the membership provider section in the web.config to some unique name? </p> <p>Also, are customErrors on? If so, where does it redirect when an error is thrown? If it's the login page, I'd suggest turning off customErrors to troubleshoot the issue. It may provide you with more information.<br> </p> <p><br> </p> 2010-10-05T13:30:32-04:00