Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Forms Authentication and using Current User Credentials programmatically

Forms Authentication and using Current User Credentials programmatically

Scheduled Pinned Locked Moved ASP.NET
securityquestion
4 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Antony M Kancidrowski
    wrote on last edited by
    #1

    Using Forms Authentication and locking down the path so you need to be authenticated to access the pages / data. I am trying to use the current authenticated user credentials in order to process a web request within the .aspx page that the user has navigated to. Code as below: HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri); req.Credentials = CredentialCache.DefaultCredentials; WebResponse res = req.GetResponse(); NOTE: the uri is valid. The response is a redirect to the login page (bad credentials redirect to login page) and not the requested data provided by the web request. I am struggling to find what I need to do in order for this request to be processed as though it was the current authenticated user. Any advice is most appreciated. Thanks in advance. Ant.

    Ant. **I'm hard, yet soft.
    I'm coloured, yet clear.
    I'm fruity and sweet.
    I'm jelly, what am I? Muse on it further, I shall return!

    **- David Walliams (Little Britain)

    M 1 Reply Last reply
    0
    • A Antony M Kancidrowski

      Using Forms Authentication and locking down the path so you need to be authenticated to access the pages / data. I am trying to use the current authenticated user credentials in order to process a web request within the .aspx page that the user has navigated to. Code as below: HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri); req.Credentials = CredentialCache.DefaultCredentials; WebResponse res = req.GetResponse(); NOTE: the uri is valid. The response is a redirect to the login page (bad credentials redirect to login page) and not the requested data provided by the web request. I am struggling to find what I need to do in order for this request to be processed as though it was the current authenticated user. Any advice is most appreciated. Thanks in advance. Ant.

      Ant. **I'm hard, yet soft.
      I'm coloured, yet clear.
      I'm fruity and sweet.
      I'm jelly, what am I? Muse on it further, I shall return!

      **- David Walliams (Little Britain)

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      Forms Authentication works (by default) by storing an encrypted ticket as a cookie upon login, which the authentication module then reads and interprets to determine the currently logged in user. For the situation you describe to work (if I am not misunderstanding you), the uri that forms your HttpWebRequest needs to either be in the same web application (which would be very unlikely) or it would be to a web application that is configured to use forms authentication with the same machineKey as the calling application. Configuring multiple applications with a common machineKey in web.config is one way to achieve single sign-on, allowing the user to have one login that passes through to other applications. You can read the following article, under "Web Farm Deployment Considerations" to see how: http://msdn.microsoft.com/en-us/library/ms998288.aspx[^] There is also a bunch of blog articles describing single signon you can find by googling "asp.net forms authentication single sign-on" Then from the calling application it would be a matter of adding the forms authentication cookie that has already been created (again through the successful submission of a user login form) to the request object's CookieContainer[^] prior to making the call. If the target application is configured correctly, it will automatically interpret the cookie and the forms authentication credentials to determine the user.

      MishaInTheCloud.blogspot.com

      A 1 Reply Last reply
      0
      • M Mike Ellison

        Forms Authentication works (by default) by storing an encrypted ticket as a cookie upon login, which the authentication module then reads and interprets to determine the currently logged in user. For the situation you describe to work (if I am not misunderstanding you), the uri that forms your HttpWebRequest needs to either be in the same web application (which would be very unlikely) or it would be to a web application that is configured to use forms authentication with the same machineKey as the calling application. Configuring multiple applications with a common machineKey in web.config is one way to achieve single sign-on, allowing the user to have one login that passes through to other applications. You can read the following article, under "Web Farm Deployment Considerations" to see how: http://msdn.microsoft.com/en-us/library/ms998288.aspx[^] There is also a bunch of blog articles describing single signon you can find by googling "asp.net forms authentication single sign-on" Then from the calling application it would be a matter of adding the forms authentication cookie that has already been created (again through the successful submission of a user login form) to the request object's CookieContainer[^] prior to making the call. If the target application is configured correctly, it will automatically interpret the cookie and the forms authentication credentials to determine the user.

        MishaInTheCloud.blogspot.com

        A Offline
        A Offline
        Antony M Kancidrowski
        wrote on last edited by
        #3

        Many thanks Mike, getting the cookie and adding it to the request CookieContainer is exactly what I wanted. Just for others to reference I have included my code. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri); // Add the current authentication cookie to the request HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; Cookie authenticationCookie = new Cookie(FormsAuthentication.FormsCookieName, cookie.Value, cookie.Path, HttpContext.Current.Request.Url.Authority); req.CookieContainer = new CookieContainer(); req.CookieContainer.Add(authenticationCookie); WebResponse res = req.GetResponse();

        Ant. **I'm hard, yet soft.
        I'm coloured, yet clear.
        I'm fruity and sweet.
        I'm jelly, what am I? Muse on it further, I shall return!

        **- David Walliams (Little Britain)

        M 1 Reply Last reply
        0
        • A Antony M Kancidrowski

          Many thanks Mike, getting the cookie and adding it to the request CookieContainer is exactly what I wanted. Just for others to reference I have included my code. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri); // Add the current authentication cookie to the request HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; Cookie authenticationCookie = new Cookie(FormsAuthentication.FormsCookieName, cookie.Value, cookie.Path, HttpContext.Current.Request.Url.Authority); req.CookieContainer = new CookieContainer(); req.CookieContainer.Add(authenticationCookie); WebResponse res = req.GetResponse();

          Ant. **I'm hard, yet soft.
          I'm coloured, yet clear.
          I'm fruity and sweet.
          I'm jelly, what am I? Muse on it further, I shall return!

          **- David Walliams (Little Britain)

          M Offline
          M Offline
          Mike Ellison
          wrote on last edited by
          #4

          I'm glad, Ant. Nice code snippet too. :cool:

          MishaInTheCloud.blogspot.com

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups