« Previous Next »

Answered Thread: Forms Authentication. Roles problem in IIS7. Please, anyone?

Last post 10-27-2009 10:44 PM by lextm. 1 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (2 items)

Sort Posts:

  • 10-27-2009, 8:56 PM

    • shapper
    • Top 150 Contributor
    • Joined on 11-28-2004, 9:15 PM
    • Posts 47

    Forms Authentication. Roles problem in IIS7. Please, anyone?

    Hello,

    I have my own membership system but that uses form authentication.

    I am able to login and logout with not problems both when I am testing the web application in VS2008 or in IIS 7.

    However, while in VS2008 testing, after login, the user gets its roles in IIS7 the user contains no roles.

    I really have no idea why this is happening ... specially because it is only Roles in IIS7!

    My Login code:

          User user = _userService.GetByUsername(username);
          if (user != null) {

            // Create authentication ticket
            FormsAuthenticationTicket ticket = new
    FormsAuthenticationTicket(1, user.Username, DateTime.UtcNow,
    DateTime.UtcNow.AddMinutes(30), true, String.Join(",",
    user.Roles.Select(r => r.Name).ToArray()),
    FormsAuthentication.FormsCookiePath);

            // Encrypt cookie with machine key
            String hash = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie
    (FormsAuthentication.FormsCookieName, hash);

            // Define experation time
            if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

            // Add cookie to outgoing response
            HttpContext.Current.Response.Cookies.Add(cookie);

    And I have the following module:

      // MembershipModule
      public class MembershipModule : IHttpModule {

        public void Init(HttpApplication application) {

          application.AuthenticateRequest += new EventHandler
    (this.OnAuthenticateRequest);

        } // Init

        public void Dispose() {
        } // Dispose

        public void OnAuthenticateRequest(Object sender, EventArgs e) {

          // Check current user
          if (HttpContext.Current.User != null) {

            // Check if authenticated
            if (HttpContext.Current.User.Identity.IsAuthenticated) {

              // Check identity
              if (HttpContext.Current.User.Identity is FormsIdentity) {

                // Define ticket
                FormsAuthenticationTicket ticket =
    FormsAuthentication.Decrypt(HttpContext.Current.Request.Cookies
    [FormsAuthentication.FormsCookieName].Value);

                // Define roles
                String[] roles = ticket.UserData.Split(',');

                // Define user
                FormsIdentity id = (FormsIdentity)
    HttpContext.Current.User.Identity;
                HttpContext.Current.User = new GenericPrincipal(id,
    roles);

              }
            }
          }

        } // OnAuthenticateRequest

    Does anyone has any idea what is going wrong?

    I tried everything I could think of but I am not able to solve this.

    Thanks,
    Miguel
  • 10-27-2009, 10:44 PM In reply to

    • lextm
    • Top 10 Contributor
    • Joined on 10-22-2008, 12:18 AM
    • Shanghai, PRC
    • Posts 1,433

    Answered Re: Forms Authentication. Roles problem in IIS7. Please, anyone?

    Hi Miguel,

    Please spare some time and review ASP.NET membership providers and role providers topics on http://www.asp.net

    When you have membership providers configured, please also configure role providers. Otherwise, ASP.NET cannot resolve roles for authenticated users.

    Lex Li
    Support Engineer at Microsoft
    ---------------------------
    This posting is provided "AS IS" with no warranties, and confers no rights.
Page 1 of 1 (2 items)
Microsoft Communities