Results 1 to 31 of 31

Thread: Imports System.Security.Principal

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216

    Imports System.Security.Principal

    How can I identify the name of the user that is browsing to my page from the network?

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Try something like
    HttpContext.CurrentUser.Identity.Name

    That was off the top of my head, but it is pretty close...

  3. #3

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Originally posted by hellswraith
    Try something like
    HttpContext.CurrentUser.Identity.Name

    That was off the top of my head, but it is pretty close...
    Well when I do it in debug the string comes up empty.

    Code:
    Dim str As String = HttpContext.Current.User.Identity.Name

  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    I believe you have to Autheticate the person first which is what the link hw posted seems to point to.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    in my web.config, I did this

    Code:
        <authorization>
            <allow users="wrbafres/candersen" /> <!-- Allow all users -->
    		<deny users="?"/>
                <!--  <allow     users="[comma separated list of users]"
                                 roles="[comma separated list of roles]"/>
                      <deny      users="[comma separated list of users]"
                                 roles="[comma separated list of roles]"/>
                -->
        </authorization>
    and httpcontext.current.user.etc.etc worked. Problem is of corse, now you would have to do it for all users. Im not surehow to do it otherwise. There might be some way.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    D'oh..silly me. I got it

    Code:
        <authentication mode="Windows" /> 
    
        <authorization>
            <deny users="?" /> <!-- Deny Anonymous
        </authorization>
    And this worked
    Code:
            Dim acc As New System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent())
            Dim username As String = wp.Identity.Name
    
            Response.Write(HttpContext.Current.User.Identity.Name.ToString())
            Response.Write(wp.Identity.Name)
    The top REsponse.write, returned my network login
    the 2nd returned the account asp.net is running under (ie mycomputer\ASPNET)
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Originally posted by Cander
    D'oh..silly me. I got it

    Code:
        <authentication mode="Windows" /> 
    
        <authorization>
            <deny users="?" /> <!-- Deny Anonymous
        </authorization>
    And this worked
    Code:
            Dim acc As New System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent())
            Dim username As String = wp.Identity.Name
    
            Response.Write(HttpContext.Current.User.Identity.Name.ToString())
            Response.Write(wp.Identity.Name)
    The top REsponse.write, returned my network login
    the 2nd returned the account asp.net is running under (ie mycomputer\ASPNET)
    Change acc to wp

  9. #9
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    oops. yeah I know. I was in the process of making the variable name more descriptive before I posted it to you. I just didnt finish.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    How do I do this authentication on specific pages and not on the whole project?

  11. #11
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    hmm. I think you will have to something like store what users/roles can access specific pages in a database or something then just do quick simple If statements and deny access to a page

    So in other words

    Code:
    If HttpContext.Current.User.Name = "Domain\Candersen" Then
    
       ' Do something
    End If
    it might be easier to map sepcific user to roles on the asp.net server, then you can check if a user is in a specific role and deny them access if not, or redirect them.
    If using the Role checking, you can do

    Code:
    If HttpContext.Current.User.IsInRole("Thiscomputer\Managers") Then
         ' balh
    
    End If
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Do I place that logic on the specific pages?

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Testing locally on the network, I can't seem to login!

  14. #14
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Originally posted by jesus4u
    How do I do this authentication on specific pages and not on the whole project?
    Create a seperate directory for the pages that require authentication. For the directory that contains pages that require authentication, add a web.config file to the directory, and specify the type of authentication.

  15. #15
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Originally posted by jesus4u
    Do I place that logic on the specific pages?
    yes
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  16. #16
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Originally posted by jesus4u
    Testing locally on the network, I can't seem to login!
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  17. #17

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    thanks for all the help but I just can't seem to login

  18. #18
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    can you post your web.config? Maybe you got something funky in there?!?!?!
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  19. #19

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Originally posted by Cander
    When I get the Win Box and enter my un and pw and domain, I get denied.

  20. #20

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Originally posted by Cander
    can you post your web.config? Maybe you got something funky in there?!?!?!
    Code:
        <authorization>
            <deny users="*"/> <!-- Allow all users -->
    
                 <allow     users="apolajenko"
                                 roles="[comma separated list of roles]"/>
                      <!-- <deny      users="[comma separated list of users]"
                                 roles="[comma separated list of roles]"/>
                -->
        </authorization

  21. #21
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    yep..soemthing funky

    deny users="*"/>

    asterix needs to be ?
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  22. #22

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Originally posted by Cander
    yep..soemthing funky

    deny users="*"/>

    asterix needs to be ?

    YEP! Got it to work! Thanks

  23. #23
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    your welcome
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  24. #24

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    What I noticed is that with this setting it allows anyone in and your other code then can read who they are on the network. But if I change the code to any other setting like <allow users="myname" then it does not authenticate like you would expect.

    Code:
    <authorization>
                        <deny users="?"/> <!-- Allow all users -->
    
                 <allow users=""
                        roles="[comma separated list of roles]"/>
                      <!-- <deny      users="[comma separated list of users]"
                                 roles="[comma separated list of roles]"/>
                -->
        </authorization>

  25. #25
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    can you show me an example of the config you say doesnt authenticate?
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  26. #26

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    it is above your last post

  27. #27
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    well if that is how you allow users looks, then of course it isnt going to wor k

    <allow users=""
    roles="[comma separated list of roles]"/>


    ???
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  28. #28

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Originally posted by Cander
    well if that is how you allow users looks, then of course it isnt going to wor k

    <allow users=""
    roles="[comma separated list of roles]"/>


    ???
    I know but even if I put my name in there it apparently lets me in but then when my buddy next to me visits the same page and he is not in the list he can still see the page when he is not supposed to.

  29. #29
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    ok..try this, change the deny user to an *, but make sure it is on the line AFTER the allow users element.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  30. #30

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Like this?
    Code:
        <authorization>
                        
                 <allow users="apolajenko"
                        roles="[comma separated list of roles]"/>
                      <!-- <deny      users="[comma separated list of users]"
                                 roles="[comma separated list of roles]"/>
                -->
                
                <deny users="*"/> <!-- Allow all users -->
        </authorization>

    Nope! I don't get authenticated

  31. #31
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    take this out
    roles="[comma separated list of roles]"
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width