HELP: Authentication code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • VB Programmer

    #1

    HELP: Authentication code

    PLEASE HELP....

    I'm having trouble. In my login form after I've verified the
    username/password are valid I do this:
    Select Case iMyPrivilege
    Case 0
    Dim arrRoles() As String = {"guest"}
    Context.User = New
    System.Security .Principal.Gene ricPrincipal(Us er.Identity, arrRoles)
    Case 1
    Dim arrRoles() As String = {"guest", "user"}
    Context.User = New
    System.Security .Principal.Gene ricPrincipal(Us er.Identity, arrRoles)
    Case 2
    Dim arrRoles() As String = {"guest", "user""admi n"}
    Context.User = New
    System.Security .Principal.Gene ricPrincipal(Us er.Identity, arrRoles)
    End Select

    In my Global.asax.vb I have this code:
    Sub Application_Aut henticateReques t(ByVal sender As Object, ByVal e As
    EventArgs)
    ' Fires upon attempting to authenticate the use
    If Request.IsAuthe nticated Then
    If Context.User.Is InRole("guest") Then
    Response.Write( "GUEST: " & Context.User.Id entity.Name)
    ElseIf Context.User.Is InRole("user") Then
    Response.Write( "USER: " & Context.User.Id entity.Name)
    ElseIf Context.User.Is InRole("admin") Then
    Response.Write( "ADMIN: " & Context.User.Id entity.Name)
    Else
    Response.Write( "????: " & Context.User.Id entity.Name)
    End If
    End If
    End Sub

    PROBLEM 1: In Application_Aut henticateReques t the If statement for
    "IsInRole" ALWAYS drops to the Else, like it doesn't recognize what I filled
    in for form login. Any ideas?

    PROBLEM 2: In my Login code I actually had "Context.Us er =" line outside
    the case statement but it kept saying "Name 'arrRoles' is not declared."
    even though I did declare it in the case statement. Any ideas?

    Thanks!


  • John Saunders

    #2
    Re: Authentication code

    "VB Programmer" <growNO-SPAM@go-intech.com> wrote in message
    news:uZ$oYCFXDH A.2200@TK2MSFTN GP09.phx.gbl...[color=blue]
    > PLEASE HELP....
    >
    > ...
    > PROBLEM 1: In Application_Aut henticateReques t the If statement for
    > "IsInRole" ALWAYS drops to the Else, like it doesn't recognize what I[/color]
    filled[color=blue]
    > in for form login. Any ideas?[/color]

    Remember that HTTP is stateless, and so is ASP.NET. By the time you get to
    Application_Aut henticateReques t, everything you ever did in Login is gone.
    You need to persist it, probably in the Forms Authentication ticket. See my
    response to your earlier post, "
    Question: COntext.User.Is InRole".
    [color=blue]
    >
    > PROBLEM 2: In my Login code I actually had "Context.Us er =" line outside
    > the case statement but it kept saying "Name 'arrRoles' is not declared."
    > even though I did declare it in the case statement. Any ideas?[/color]

    It looks like the case clauses each introduce a new scope. Did you notice
    that you were able to declare the same name three times? When that case
    clause is done, the scope is gone, and so are any variables declared in that
    scope. Declare your array before the "Select" and just set it in each Case
    clause.
    --
    John Saunders
    Internet Engineer
    john.saunders@s urfcontrol.com


    Comment

    • VB Programmer

      #3
      HOW TO: Setting Up Forms Authentication

      To use forms authentication. ..

      1. Modify <Web.config>
      Turn on forms authentication. ..
      <authenticati on mode="Forms">
      <forms name=".ASPXAUTH " loginUrl="Login .aspx" />
      </authentication>
      <authorizatio n>
      <deny users="?" />
      </authorization>

      Insert before the end of the file add the section for Secured dir....
      <location path="Secured">
      <system.web>
      <authorizatio n>
      <allow roles="admin"/>
      <deny users="*" />
      </authorization>
      </system.web>
      </location>

      2. Login.aspx
      After user is verified (in db, xml, etc...) add this:

      System.Web.Secu rity.FormsAuthe ntication.Redir ectFromLoginPag e(txtUserName.T e
      xt.Trim, True)

      3. Global.asax.vb
      First add imports statement "Imports System.Security .Principal"

      Then...
      Sub Application_Aut henticateReques t(ByVal sender As Object, ByVal e As
      EventArgs)
      ' Fires upon attempting to authenticate the use
      If Request.IsAuthe nticated Then
      ' Get the user's role
      Dim cnnMyConnection As SqlConnection = New
      SqlConnection(C onfigurationSet tings.AppSettin gs("MyDsnString "))
      Dim cmdMyCmd As SqlCommand = New SqlCommand("SEL ECT blah FROM
      blah WHERE blah", cnnMyConnection )
      Dim drUsers As SqlDataReader

      cnnMyConnection .Open()
      drUsers = cmdMyCmd.Execut eReader

      While drUsers.Read
      Select Case drUsers.GetValu e(1)
      Case 0 ' guest (read only)
      Dim arrRoles() As String = {"guest"}
      Context.User = New
      System.Security .Principal.Gene ricPrincipal(Us er.Identity, arrRoles)
      Case 1 ' user (start/stop engines)
      Dim arrRoles() As String = {"guest", "user"}
      Context.User = New
      System.Security .Principal.Gene ricPrincipal(Us er.Identity, arrRoles)
      Case 2 ' admin (everything)
      Dim arrRoles() As String = {"guest", "user",
      "admin"}
      Context.User = New
      System.Security .Principal.Gene ricPrincipal(Us er.Identity, arrRoles)
      End Select
      End While

      cnnMyConnection .Close()

      'If Context.User.Is InRole("guest") Then Response.Write( "GUEST "
      & Context.User.Id entity.Name)
      End If
      End Sub


      Comment

      • John Saunders

        #4
        Re: HOW TO: Setting Up Forms Authentication

        "VB Programmer" <growNO-SPAM@go-intech.com> wrote in message
        news:eO9%23dmFX DHA.2312@TK2MSF TNGP10.phx.gbl. ..[color=blue]
        > To use forms authentication. ..
        >[/color]
        ....[color=blue]
        > 3. Global.asax.vb
        > First add imports statement "Imports System.Security .Principal"
        >
        > Then...
        > Sub Application_Aut henticateReques t(ByVal sender As Object, ByVal e As
        > EventArgs)
        > ' Fires upon attempting to authenticate the use
        > If Request.IsAuthe nticated Then
        > ' Get the user's role
        > Dim cnnMyConnection As SqlConnection = New
        > SqlConnection(C onfigurationSet tings.AppSettin gs("MyDsnString "))
        > Dim cmdMyCmd As SqlCommand = New SqlCommand("SEL ECT blah FROM
        > blah WHERE blah", cnnMyConnection )
        > Dim drUsers As SqlDataReader
        >
        > cnnMyConnection .Open()
        > drUsers = cmdMyCmd.Execut eReader
        >
        > While drUsers.Read
        > Select Case drUsers.GetValu e(1)
        > Case 0 ' guest (read only)
        > Dim arrRoles() As String = {"guest"}
        > Context.User = New
        > System.Security .Principal.Gene ricPrincipal(Us er.Identity, arrRoles)
        > Case 1 ' user (start/stop engines)
        > Dim arrRoles() As String = {"guest", "user"}
        > Context.User = New
        > System.Security .Principal.Gene ricPrincipal(Us er.Identity, arrRoles)
        > Case 2 ' admin (everything)
        > Dim arrRoles() As String = {"guest", "user",
        > "admin"}
        > Context.User = New
        > System.Security .Principal.Gene ricPrincipal(Us er.Identity, arrRoles)
        > End Select
        > End While
        >
        > cnnMyConnection .Close()
        >
        > 'If Context.User.Is InRole("guest") Then Response.Write( "GUEST[/color]
        "[color=blue]
        > & Context.User.Id entity.Name)
        > End If
        > End Sub
        >
        >[/color]

        Your code will work fine, and will run on every request made to a page in
        your web application. That's a lot of database work.

        I suggest you put the database code into Login, save the resultant roles in
        the UserData of the Forms Authentication Ticket, and retrieve them in
        Application_Aut henticateReques t.
        --
        John Saunders
        Internet Engineer
        john.saunders@s urfcontrol.com


        Comment

        • VB Programmer

          #5
          Re: HOW TO: Setting Up Forms Authentication

          Should I put that code in my login form or global.asax.vb?

          "John Saunders" <john.saunders@ surfcontrol.com > wrote in message
          news:u12tQvFXDH A.1896@TK2MSFTN GP12.phx.gbl...[color=blue]
          > "VB Programmer" <growNO-SPAM@go-intech.com> wrote in message
          > news:eO9%23dmFX DHA.2312@TK2MSF TNGP10.phx.gbl. ..[color=green]
          > > To use forms authentication. ..
          > >[/color]
          > ...[color=green]
          > > 3. Global.asax.vb
          > > First add imports statement "Imports System.Security .Principal"
          > >
          > > Then...
          > > Sub Application_Aut henticateReques t(ByVal sender As Object, ByVal e[/color][/color]
          As[color=blue][color=green]
          > > EventArgs)
          > > ' Fires upon attempting to authenticate the use
          > > If Request.IsAuthe nticated Then
          > > ' Get the user's role
          > > Dim cnnMyConnection As SqlConnection = New
          > > SqlConnection(C onfigurationSet tings.AppSettin gs("MyDsnString "))
          > > Dim cmdMyCmd As SqlCommand = New SqlCommand("SEL ECT blah[/color][/color]
          FROM[color=blue][color=green]
          > > blah WHERE blah", cnnMyConnection )
          > > Dim drUsers As SqlDataReader
          > >
          > > cnnMyConnection .Open()
          > > drUsers = cmdMyCmd.Execut eReader
          > >
          > > While drUsers.Read
          > > Select Case drUsers.GetValu e(1)
          > > Case 0 ' guest (read only)
          > > Dim arrRoles() As String = {"guest"}
          > > Context.User = New
          > > System.Security .Principal.Gene ricPrincipal(Us er.Identity, arrRoles)
          > > Case 1 ' user (start/stop engines)
          > > Dim arrRoles() As String = {"guest", "user"}
          > > Context.User = New
          > > System.Security .Principal.Gene ricPrincipal(Us er.Identity, arrRoles)
          > > Case 2 ' admin (everything)
          > > Dim arrRoles() As String = {"guest", "user",
          > > "admin"}
          > > Context.User = New
          > > System.Security .Principal.Gene ricPrincipal(Us er.Identity, arrRoles)
          > > End Select
          > > End While
          > >
          > > cnnMyConnection .Close()
          > >
          > > 'If Context.User.Is InRole("guest") Then[/color][/color]
          Response.Write( "GUEST[color=blue]
          > "[color=green]
          > > & Context.User.Id entity.Name)
          > > End If
          > > End Sub
          > >
          > >[/color]
          >
          > Your code will work fine, and will run on every request made to a page in
          > your web application. That's a lot of database work.
          >
          > I suggest you put the database code into Login, save the resultant roles[/color]
          in[color=blue]
          > the UserData of the Forms Authentication Ticket, and retrieve them in
          > Application_Aut henticateReques t.
          > --
          > John Saunders
          > Internet Engineer
          > john.saunders@s urfcontrol.com
          >
          >[/color]


          Comment

          • VB Programmer

            #6
            Re: HOW TO: Setting Up Forms Authentication

            Cool. That's basically what I did.

            1. What defines where the custom cookie is stored? I used to see the
            default cookie in "C:\Documen ts and Settings\Admini strator\Cookies ", but now
            I can't find my custom cookie?

            2. How do I retrieve the roles that are stored in UserData (ticket)?

            3. What is a common reason why you would access this in
            Application_Aut henticateReques t? This seems to work with no code in
            Application_Aut henticateReques t.

            You're a great resource! Thanks.

            "John Saunders" <john.saunders@ surfcontrol.com > wrote in message
            news:OxXoyRPXDH A.3248@tk2msftn gp13.phx.gbl...[color=blue]
            > "VB Programmer" <growNO-SPAM@go-intech.com> wrote in message
            > news:%23yM8NLPX DHA.388@TK2MSFT NGP10.phx.gbl.. .[color=green]
            > > Should I put that code in my login form or global.asax.vb?[/color]
            >
            > I suggest you put the database code into Login, save the resultant roles
            > into the UserData of the Forms Authentication Ticket, and retrieve them in
            > Application_Aut henticateReques t.
            >[color=green]
            > >
            > > "John Saunders" <john.saunders@ surfcontrol.com > wrote in message
            > > news:u12tQvFXDH A.1896@TK2MSFTN GP12.phx.gbl...[color=darkred]
            > > > "VB Programmer" <growNO-SPAM@go-intech.com> wrote in message
            > > > news:eO9%23dmFX DHA.2312@TK2MSF TNGP10.phx.gbl. ..
            > > > > To use forms authentication. ..
            > > > >
            > > > ...
            > > > > 3. Global.asax.vb
            > > > > First add imports statement "Imports System.Security .Principal"
            > > > >
            > > > > Then...
            > > > > Sub Application_Aut henticateReques t(ByVal sender As Object,[/color][/color][/color]
            ByVal[color=blue]
            > e[color=green]
            > > As[color=darkred]
            > > > > EventArgs)
            > > > > ' Fires upon attempting to authenticate the use
            > > > > If Request.IsAuthe nticated Then
            > > > > ' Get the user's role
            > > > > Dim cnnMyConnection As SqlConnection = New
            > > > > SqlConnection(C onfigurationSet tings.AppSettin gs("MyDsnString "))
            > > > > Dim cmdMyCmd As SqlCommand = New SqlCommand("SEL ECT blah[/color]
            > > FROM[color=darkred]
            > > > > blah WHERE blah", cnnMyConnection )
            > > > > Dim drUsers As SqlDataReader
            > > > >
            > > > > cnnMyConnection .Open()
            > > > > drUsers = cmdMyCmd.Execut eReader
            > > > >
            > > > > While drUsers.Read
            > > > > Select Case drUsers.GetValu e(1)
            > > > > Case 0 ' guest (read only)
            > > > > Dim arrRoles() As String = {"guest"}
            > > > > Context.User = New
            > > > > System.Security .Principal.Gene ricPrincipal(Us er.Identity, arrRoles)
            > > > > Case 1 ' user (start/stop engines)
            > > > > Dim arrRoles() As String = {"guest", "user"}
            > > > > Context.User = New
            > > > > System.Security .Principal.Gene ricPrincipal(Us er.Identity, arrRoles)
            > > > > Case 2 ' admin (everything)
            > > > > Dim arrRoles() As String = {"guest", "user",
            > > > > "admin"}
            > > > > Context.User = New
            > > > > System.Security .Principal.Gene ricPrincipal(Us er.Identity, arrRoles)
            > > > > End Select
            > > > > End While
            > > > >
            > > > > cnnMyConnection .Close()
            > > > >
            > > > > 'If Context.User.Is InRole("guest") Then[/color]
            > > Response.Write( "GUEST[color=darkred]
            > > > "
            > > > > & Context.User.Id entity.Name)
            > > > > End If
            > > > > End Sub
            > > > >
            > > > >
            > > >
            > > > Your code will work fine, and will run on every request made to a page[/color][/color]
            > in[color=green][color=darkred]
            > > > your web application. That's a lot of database work.
            > > >
            > > > I suggest you put the database code into Login, save the resultant[/color][/color][/color]
            roles[color=blue][color=green]
            > > in[color=darkred]
            > > > the UserData of the Forms Authentication Ticket, and retrieve them in
            > > > Application_Aut henticateReques t.
            > > > --
            > > > John Saunders
            > > > Internet Engineer
            > > > john.saunders@s urfcontrol.com
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • VB Programmer

              #7
              Re: HOW TO: Setting Up Forms Authentication

              Please answer #1 and #2.

              Ignore #3: I figured out that this is where you need to setup the new
              GenericPrincipa l BASED on the role that is stored in the UserData (in the
              cookie).... I think. ;)

              "VB Programmer" <growNO-SPAM@go-intech.com> wrote in message
              news:uAMxYlPXDH A.1896@TK2MSFTN GP12.phx.gbl...[color=blue]
              > Cool. That's basically what I did.
              >
              > 1. What defines where the custom cookie is stored? I used to see the
              > default cookie in "C:\Documen ts and Settings\Admini strator\Cookies ", but[/color]
              now[color=blue]
              > I can't find my custom cookie?
              >
              > 2. How do I retrieve the roles that are stored in UserData (ticket)?
              >
              > 3. What is a common reason why you would access this in
              > Application_Aut henticateReques t? This seems to work with no code in
              > Application_Aut henticateReques t.
              >
              > You're a great resource! Thanks.
              >
              > "John Saunders" <john.saunders@ surfcontrol.com > wrote in message
              > news:OxXoyRPXDH A.3248@tk2msftn gp13.phx.gbl...[color=green]
              > > "VB Programmer" <growNO-SPAM@go-intech.com> wrote in message
              > > news:%23yM8NLPX DHA.388@TK2MSFT NGP10.phx.gbl.. .[color=darkred]
              > > > Should I put that code in my login form or global.asax.vb?[/color]
              > >
              > > I suggest you put the database code into Login, save the resultant roles
              > > into the UserData of the Forms Authentication Ticket, and retrieve them[/color][/color]
              in[color=blue][color=green]
              > > Application_Aut henticateReques t.
              > >[color=darkred]
              > > >
              > > > "John Saunders" <john.saunders@ surfcontrol.com > wrote in message
              > > > news:u12tQvFXDH A.1896@TK2MSFTN GP12.phx.gbl...
              > > > > "VB Programmer" <growNO-SPAM@go-intech.com> wrote in message
              > > > > news:eO9%23dmFX DHA.2312@TK2MSF TNGP10.phx.gbl. ..
              > > > > > To use forms authentication. ..
              > > > > >
              > > > > ...
              > > > > > 3. Global.asax.vb
              > > > > > First add imports statement "Imports System.Security .Principal"
              > > > > >
              > > > > > Then...
              > > > > > Sub Application_Aut henticateReques t(ByVal sender As Object,[/color][/color]
              > ByVal[color=green]
              > > e[color=darkred]
              > > > As
              > > > > > EventArgs)
              > > > > > ' Fires upon attempting to authenticate the use
              > > > > > If Request.IsAuthe nticated Then
              > > > > > ' Get the user's role
              > > > > > Dim cnnMyConnection As SqlConnection = New
              > > > > > SqlConnection(C onfigurationSet tings.AppSettin gs("MyDsnString "))
              > > > > > Dim cmdMyCmd As SqlCommand = New SqlCommand("SEL ECT[/color][/color][/color]
              blah[color=blue][color=green][color=darkred]
              > > > FROM
              > > > > > blah WHERE blah", cnnMyConnection )
              > > > > > Dim drUsers As SqlDataReader
              > > > > >
              > > > > > cnnMyConnection .Open()
              > > > > > drUsers = cmdMyCmd.Execut eReader
              > > > > >
              > > > > > While drUsers.Read
              > > > > > Select Case drUsers.GetValu e(1)
              > > > > > Case 0 ' guest (read only)
              > > > > > Dim arrRoles() As String = {"guest"}
              > > > > > Context.User = New
              > > > > > System.Security .Principal.Gene ricPrincipal(Us er.Identity,[/color][/color][/color]
              arrRoles)[color=blue][color=green][color=darkred]
              > > > > > Case 1 ' user (start/stop engines)
              > > > > > Dim arrRoles() As String = {"guest",[/color][/color][/color]
              "user"}[color=blue][color=green][color=darkred]
              > > > > > Context.User = New
              > > > > > System.Security .Principal.Gene ricPrincipal(Us er.Identity,[/color][/color][/color]
              arrRoles)[color=blue][color=green][color=darkred]
              > > > > > Case 2 ' admin (everything)
              > > > > > Dim arrRoles() As String = {"guest",[/color][/color][/color]
              "user",[color=blue][color=green][color=darkred]
              > > > > > "admin"}
              > > > > > Context.User = New
              > > > > > System.Security .Principal.Gene ricPrincipal(Us er.Identity,[/color][/color][/color]
              arrRoles)[color=blue][color=green][color=darkred]
              > > > > > End Select
              > > > > > End While
              > > > > >
              > > > > > cnnMyConnection .Close()
              > > > > >
              > > > > > 'If Context.User.Is InRole("guest") Then
              > > > Response.Write( "GUEST
              > > > > "
              > > > > > & Context.User.Id entity.Name)
              > > > > > End If
              > > > > > End Sub
              > > > > >
              > > > > >
              > > > >
              > > > > Your code will work fine, and will run on every request made to a[/color][/color][/color]
              page[color=blue][color=green]
              > > in[color=darkred]
              > > > > your web application. That's a lot of database work.
              > > > >
              > > > > I suggest you put the database code into Login, save the resultant[/color][/color]
              > roles[color=green][color=darkred]
              > > > in
              > > > > the UserData of the Forms Authentication Ticket, and retrieve them[/color][/color][/color]
              in[color=blue][color=green][color=darkred]
              > > > > Application_Aut henticateReques t.
              > > > > --
              > > > > John Saunders
              > > > > Internet Engineer
              > > > > john.saunders@s urfcontrol.com
              > > > >
              > > > >
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • John Saunders

                #8
                Re: HOW TO: Setting Up Forms Authentication

                "VB Programmer" <growNO-SPAM@go-intech.com> wrote in message
                news:uAMxYlPXDH A.1896@TK2MSFTN GP12.phx.gbl...[color=blue]
                > Cool. That's basically what I did.
                >
                > 1. What defines where the custom cookie is stored? I used to see the
                > default cookie in "C:\Documen ts and Settings\Admini strator\Cookies ", but[/color]
                now[color=blue]
                > I can't find my custom cookie?[/color]

                If you don't set an expiration date on a cookie, it will be a "session
                cookie", which I don't believe is stored on disk. Session cookies are a Good
                Thing, as browsers are more likely to be set to accept them than permanent
                cookies.
                [color=blue]
                > 2. How do I retrieve the roles that are stored in UserData (ticket)?[/color]

                By doing the opposite of of what you did to put them there. :-)

                For instance, if your database code in login produced an array of roles, you
                might use:

                string[] roles = GetRolesForUser (userName);
                string userData = String.Join("," , roles);
                FormsAuthentica tionTicket ticket = new FormsAuthentica tionTicket(
                1,
                userName,
                System.DateTime .Now,
                System.DateTime .Now.AddMinutes (30),
                isPersistent,
                userData,
                FormsAuthentica tion.FormsCooki ePath);

                // Encrypt the ticket.
                string encTicket = FormsAuthentica tion.Encrypt(ti cket);

                // Create the cookie.
                Response.Cookie s.Add(new HttpCookie(Form sAuthentication .FormsCookieNam e,
                encTicket));

                // Redirect back to original URL.
                Response.Redire ct(FormsAuthent ication.GetRedi rectUrl(userNam e,isPersistent) )
                ;


                Well, in this case you'll want to do the following in
                Application_Aut henticateReques t:

                FormsIdentity fi = User.Identity as FormsIdentity;
                if (fi == null) return; // don't know how _that_ happened!
                FormsAuthentica tionTicket ticket = fi.Ticket;
                string userData = ticket.UserData ;
                string roles[] = userData.Split( ',');
                Request.User = new GenericPrincipa l(fi, roles);
                [color=blue]
                > 3. What is a common reason why you would access this in
                > Application_Aut henticateReques t? This seems to work with no code in
                > Application_Aut henticateReques t.[/color]

                But it's not working. If you put the user in a role right now, is he still
                in the same role on all subsequent requests? I doubt it. You need to set the
                Principal on each request - remember we're talking "stateless" .
                --
                John Saunders
                Internet Engineer
                john.saunders@s urfcontrol.com
                [color=blue]
                > You're a great resource! Thanks.[/color]

                You're welcome.

                --
                John Saunders
                Internet Engineer
                john.saunders@s urfcontrol.com


                Comment

                • VB Programmer

                  #9
                  HOW TO: Setting Up Forms Authentication (Revised)

                  John, it works like a champ. Thanks for ALL of your help! ;)

                  FYI, this is what I changed...

                  (1) In my login page it calls...
                  Public Sub RedirectFromLog inPage(ByVal strUserName As String, ByVal
                  strUserData As String, ByVal strDefaultRedir ectUrl As String)
                  Dim ctxMyContext As HttpContext = HttpContext.Cur rent
                  Dim fatTicket As New FormsAuthentica tionTicket( _
                  1, txtUserName.Tex t.ToUpper.Trim, DateTime.Now, _
                  DateTime.Now.Ad dMinutes(30), False, strUserData)
                  Dim strCookieValue As String =
                  FormsAuthentica tion.Encrypt(fa tTicket)
                  Dim cookieMyCookie As HttpCookie = New
                  HttpCookie(Form sAuthentication .FormsCookieNam e)
                  Dim strReturnUrl As String

                  With cookieMyCookie
                  .Path = FormsAuthentica tion.FormsCooki ePath
                  .Value = strCookieValue
                  .Expires = DateTime.Now.Ad dMinutes(30)
                  End With
                  ctxMyContext.Re sponse.Cookies. Add(cookieMyCoo kie)

                  If ctxMyContext.Re quest.QueryStri ng("ReturnUrl" ) Is Nothing Then
                  strReturnUrl = strDefaultRedir ectUrl
                  Else
                  strReturnUrl = ctxMyContext.Re quest.QueryStri ng("ReturnUrl" )
                  End If

                  ctxMyContext.Re sponse.Redirect (strReturnUrl)
                  End Sub

                  (2)
                  Sub Application_Aut henticateReques t(ByVal sender As Object, ByVal e As
                  EventArgs)
                  ' Fires upon attempting to authenticate the use
                  If Request.IsAuthe nticated Then
                  Dim fiIndentity As FormsIdentity = CType(User.Iden tity,
                  FormsIdentity)
                  If fiIndentity Is Nothing Then Exit Sub

                  Dim fatTicket As Security.FormsA uthenticationTi cket =
                  fiIndentity.Tic ket
                  Dim strUserData As String = fatTicket.UserD ata

                  Select Case strUserData
                  Case "guest"
                  Dim arrRoles() As String = {"guest"}
                  Context.User = New
                  System.Security .Principal.Gene ricPrincipal(fi Indentity, arrRoles)
                  Case "user"
                  Dim arrRoles() As String = {"guest", "user"}
                  Context.User = New
                  System.Security .Principal.Gene ricPrincipal(fi Indentity, arrRoles)
                  Case "admin"
                  Dim arrRoles() As String = {"guest", "user", "admin"}
                  Context.User = New
                  System.Security .Principal.Gene ricPrincipal(fi Indentity, arrRoles)
                  End Select
                  End If
                  End Sub


                  Comment

                  • John Saunders

                    #10
                    Re: HOW TO: Setting Up Forms Authentication (Revised)

                    This looks good, but one thing was lost in the translation. Ctype doesn't do
                    the same thing as the "as" operator does in C#.

                    "object as Type" will return null (Nothing) if object cannot be cast to
                    Type, otherwise it will do the cast and return the result. On the other
                    hand, if somehow User.Identify were not a FormsIdentify, CType would throw
                    an exception.

                    --
                    John Saunders
                    Internet Engineer
                    john.saunders@s urfcontrol.com


                    "VB Programmer" <growNO-SPAM@go-intech.com> wrote in message
                    news:%233AE5eQX DHA.2204@TK2MSF TNGP12.phx.gbl. ..[color=blue]
                    > John, it works like a champ. Thanks for ALL of your help! ;)
                    >
                    > FYI, this is what I changed...
                    >
                    > (1) In my login page it calls...
                    > Public Sub RedirectFromLog inPage(ByVal strUserName As String, ByVal
                    > strUserData As String, ByVal strDefaultRedir ectUrl As String)
                    > Dim ctxMyContext As HttpContext = HttpContext.Cur rent
                    > Dim fatTicket As New FormsAuthentica tionTicket( _
                    > 1, txtUserName.Tex t.ToUpper.Trim, DateTime.Now, _
                    > DateTime.Now.Ad dMinutes(30), False, strUserData)
                    > Dim strCookieValue As String =
                    > FormsAuthentica tion.Encrypt(fa tTicket)
                    > Dim cookieMyCookie As HttpCookie = New
                    > HttpCookie(Form sAuthentication .FormsCookieNam e)
                    > Dim strReturnUrl As String
                    >
                    > With cookieMyCookie
                    > .Path = FormsAuthentica tion.FormsCooki ePath
                    > .Value = strCookieValue
                    > .Expires = DateTime.Now.Ad dMinutes(30)
                    > End With
                    > ctxMyContext.Re sponse.Cookies. Add(cookieMyCoo kie)
                    >
                    > If ctxMyContext.Re quest.QueryStri ng("ReturnUrl" ) Is Nothing Then
                    > strReturnUrl = strDefaultRedir ectUrl
                    > Else
                    > strReturnUrl = ctxMyContext.Re quest.QueryStri ng("ReturnUrl" )
                    > End If
                    >
                    > ctxMyContext.Re sponse.Redirect (strReturnUrl)
                    > End Sub
                    >
                    > (2)
                    > Sub Application_Aut henticateReques t(ByVal sender As Object, ByVal e As
                    > EventArgs)
                    > ' Fires upon attempting to authenticate the use
                    > If Request.IsAuthe nticated Then
                    > Dim fiIndentity As FormsIdentity = CType(User.Iden tity,
                    > FormsIdentity)
                    > If fiIndentity Is Nothing Then Exit Sub
                    >
                    > Dim fatTicket As Security.FormsA uthenticationTi cket =
                    > fiIndentity.Tic ket
                    > Dim strUserData As String = fatTicket.UserD ata
                    >
                    > Select Case strUserData
                    > Case "guest"
                    > Dim arrRoles() As String = {"guest"}
                    > Context.User = New
                    > System.Security .Principal.Gene ricPrincipal(fi Indentity, arrRoles)
                    > Case "user"
                    > Dim arrRoles() As String = {"guest", "user"}
                    > Context.User = New
                    > System.Security .Principal.Gene ricPrincipal(fi Indentity, arrRoles)
                    > Case "admin"
                    > Dim arrRoles() As String = {"guest", "user", "admin"}
                    > Context.User = New
                    > System.Security .Principal.Gene ricPrincipal(fi Indentity, arrRoles)
                    > End Select
                    > End If
                    > End Sub
                    >
                    >[/color]


                    Comment

                    • VB Programmer

                      #11
                      Re: HOW TO: Setting Up Forms Authentication (Revised)

                      The reason I changed it from...
                      Dim fiIndentity As FormsIdentity = User.Identity
                      to...
                      Dim fiIndentity As FormsIdentity = CType(User.Iden tity, FormsIdentity)
                      ....was that I go a squiggly under User.Identity stating "Option Strict On
                      disallows implicit conversions from 'System.Securit y.Principle.Iid entity' to
                      'System.Web.Sec urity.FormsIden tity.'" Should I do it an alternate way?

                      "John Saunders" <john.saunders@ surfcontrol.com > wrote in message
                      news:OEjapoQXDH A.384@TK2MSFTNG P12.phx.gbl...[color=blue]
                      > This looks good, but one thing was lost in the translation. Ctype doesn't[/color]
                      do[color=blue]
                      > the same thing as the "as" operator does in C#.
                      >
                      > "object as Type" will return null (Nothing) if object cannot be cast to
                      > Type, otherwise it will do the cast and return the result. On the other
                      > hand, if somehow User.Identify were not a FormsIdentify, CType would throw
                      > an exception.
                      >
                      > --
                      > John Saunders
                      > Internet Engineer
                      > john.saunders@s urfcontrol.com
                      >
                      >
                      > "VB Programmer" <growNO-SPAM@go-intech.com> wrote in message
                      > news:%233AE5eQX DHA.2204@TK2MSF TNGP12.phx.gbl. ..[color=green]
                      > > John, it works like a champ. Thanks for ALL of your help! ;)
                      > >
                      > > FYI, this is what I changed...
                      > >
                      > > (1) In my login page it calls...
                      > > Public Sub RedirectFromLog inPage(ByVal strUserName As String, ByVal
                      > > strUserData As String, ByVal strDefaultRedir ectUrl As String)
                      > > Dim ctxMyContext As HttpContext = HttpContext.Cur rent
                      > > Dim fatTicket As New FormsAuthentica tionTicket( _
                      > > 1, txtUserName.Tex t.ToUpper.Trim, DateTime.Now, _
                      > > DateTime.Now.Ad dMinutes(30), False, strUserData)
                      > > Dim strCookieValue As String =
                      > > FormsAuthentica tion.Encrypt(fa tTicket)
                      > > Dim cookieMyCookie As HttpCookie = New
                      > > HttpCookie(Form sAuthentication .FormsCookieNam e)
                      > > Dim strReturnUrl As String
                      > >
                      > > With cookieMyCookie
                      > > .Path = FormsAuthentica tion.FormsCooki ePath
                      > > .Value = strCookieValue
                      > > .Expires = DateTime.Now.Ad dMinutes(30)
                      > > End With
                      > > ctxMyContext.Re sponse.Cookies. Add(cookieMyCoo kie)
                      > >
                      > > If ctxMyContext.Re quest.QueryStri ng("ReturnUrl" ) Is Nothing Then
                      > > strReturnUrl = strDefaultRedir ectUrl
                      > > Else
                      > > strReturnUrl = ctxMyContext.Re quest.QueryStri ng("ReturnUrl" )
                      > > End If
                      > >
                      > > ctxMyContext.Re sponse.Redirect (strReturnUrl)
                      > > End Sub
                      > >
                      > > (2)
                      > > Sub Application_Aut henticateReques t(ByVal sender As Object, ByVal e[/color][/color]
                      As[color=blue][color=green]
                      > > EventArgs)
                      > > ' Fires upon attempting to authenticate the use
                      > > If Request.IsAuthe nticated Then
                      > > Dim fiIndentity As FormsIdentity = CType(User.Iden tity,
                      > > FormsIdentity)
                      > > If fiIndentity Is Nothing Then Exit Sub
                      > >
                      > > Dim fatTicket As Security.FormsA uthenticationTi cket =
                      > > fiIndentity.Tic ket
                      > > Dim strUserData As String = fatTicket.UserD ata
                      > >
                      > > Select Case strUserData
                      > > Case "guest"
                      > > Dim arrRoles() As String = {"guest"}
                      > > Context.User = New
                      > > System.Security .Principal.Gene ricPrincipal(fi Indentity, arrRoles)
                      > > Case "user"
                      > > Dim arrRoles() As String = {"guest", "user"}
                      > > Context.User = New
                      > > System.Security .Principal.Gene ricPrincipal(fi Indentity, arrRoles)
                      > > Case "admin"
                      > > Dim arrRoles() As String = {"guest", "user",[/color][/color]
                      "admin"}[color=blue][color=green]
                      > > Context.User = New
                      > > System.Security .Principal.Gene ricPrincipal(fi Indentity, arrRoles)
                      > > End Select
                      > > End If
                      > > End Sub
                      > >
                      > >[/color]
                      >
                      >[/color]


                      Comment

                      • John Saunders

                        #12
                        Re: HOW TO: Setting Up Forms Authentication (Revised)

                        "VB Programmer" <growNO-SPAM@go-intech.com> wrote in message
                        news:OlJPBSRXDH A.2476@tk2msftn gp13.phx.gbl...[color=blue]
                        > The reason I changed it from...
                        > Dim fiIndentity As FormsIdentity = User.Identity
                        > to...
                        > Dim fiIndentity As FormsIdentity = CType(User.Iden tity, FormsIdentity)
                        > ...was that I go a squiggly under User.Identity stating "Option Strict On
                        > disallows implicit conversions from 'System.Securit y.Principle.Iid entity'[/color]
                        to[color=blue]
                        > 'System.Web.Sec urity.FormsIden tity.'" Should I do it an alternate way?[/color]

                        No, but you should first check to make sure it's a FormsIdentity - and I
                        can't remember right now how VB.NET does that!
                        --
                        John Saunders
                        Internet Engineer
                        john.saunders@s urfcontrol.com



                        Comment

                        Working...