customize the httpcontext.current.user

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

    #1

    customize the httpcontext.current.user

    Hi all
    Does anyone has any reference or examples about how to customize the
    httpcontext.cur rent.user object to my own user object?

    Cheers
    Victor


  • Walter Wang [MSFT]

    #2
    RE: customize the httpcontext.cur rent.user

    Hi Victor,

    HttpContext.Cur rent.User is of type IPrincipal; a Principal object contains
    Identities and Roles. You can find an article on how to implement
    IPrincipal in ASP.NET:

    #Building Secure ASP.NET Applications: Authentication, Authorization, and
    Secure Communication
    http://msdn2.microsoft.com/en-us/library/aa302401.aspx

    Please feel free to let me know if you have anything unclear. Thanks.


    Sincerely,
    Walter Wang (wawang@online. microsoft.com, remove 'online.')
    Microsoft Online Community Support

    =============== =============== =============== =====
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscripti...ult.aspx#notif
    ications. If you are using Outlook Express, please make sure you clear the
    check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
    promptly.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscripti...t/default.aspx.
    =============== =============== =============== =====

    This posting is provided "AS IS" with no warranties, and confers no rights.

    Comment

    • Walter Wang [MSFT]

      #3
      RE: customize the httpcontext.cur rent.user

      Hi Victor,

      Have you seen above suggestion? Would you please let me know the status of
      this post? Thanks.


      Regards,
      Walter Wang (wawang@online. microsoft.com, remove 'online.')
      Microsoft Online Community Support

      =============== =============== =============== =====
      When responding to posts, please "Reply to Group" via your newsreader so
      that others may learn and benefit from your issue.
      =============== =============== =============== =====

      This posting is provided "AS IS" with no warranties, and confers no rights.

      Comment

      • Victor

        #4
        Re: customize the httpcontext.cur rent.user

        Hi Walter:
        Thanks for the reply. What I want is more complicated. I have a customized
        user object. (which contains infomation about the user'a name gender email
        homeaddress.. all sorts of the user detail info) The thing I want to achieve
        is to store all the info in someplace (perfer not session) so that I can
        access these info without accessing database everytime. I tried to overwrite
        the httpcontext.cur rent.user object. but i found after a new request, all my
        infomation will be lost. only that three fields for the iidentity class left
        (Name, IsAuthenticated ,Authentication Type ). And I dont know how to fix
        that. Now what i did is save my user infomation in the
        httpcontext.cur rent.items collection. that means i have initilize my user
        object for each request, which is not exactly i want, but better than access
        db everytime i try to use the user object.
        Before that I also try to save the info in the thread's name slot. but i
        found it's not stable when i use ajax in my website.

        Do you know any other way to achieve my goal?

        Thanks a lot :)
        Victor

        "Walter Wang [MSFT]" <wawang@online. microsoft.comwr ote in message
        news:UnP4dKxeHH A.4368@TK2MSFTN GHUB02.phx.gbl. ..
        Hi Victor,
        >
        Have you seen above suggestion? Would you please let me know the status of
        this post? Thanks.
        >
        >
        Regards,
        Walter Wang (wawang@online. microsoft.com, remove 'online.')
        Microsoft Online Community Support
        >
        =============== =============== =============== =====
        When responding to posts, please "Reply to Group" via your newsreader so
        that others may learn and benefit from your issue.
        =============== =============== =============== =====
        >
        This posting is provided "AS IS" with no warranties, and confers no
        rights.
        >

        Comment

        • Walter Wang [MSFT]

          #5
          Re: customize the httpcontext.cur rent.user

          Hi Victor,

          I don't understand why the custom IPrincipal object will lose custom
          information, unless the IPrincipal object is not the custom one already. I
          mean: if your class implements IPrincipal and create the instance in
          Application_Aut henticateReques t and assign to Context.User, it should keep
          the reference and you can always cast it to your custom class when used. If
          you only see the Name, IsAuthenticated , AuthenticationT ype properties,
          that's because the reference type is IPrincipal. You need to cast it to
          your custom class:

          CustomPrincipal myPrincipal = (CustomPrincipa l) HttpContext.Cur rent.User;


          Above code is about the custom IPrincipal, I understand you're implementing
          a custom IIdentity, which is accessed from IPrincipal.Iden tity, which also
          need to cast to your cusom implementation class to access those custom
          information you added.


          Take a step back to look at your question, I understand you only need to
          store some information for the current logined user, I think customizing
          the IPrincipal and IIdentity may not be needed. Based on my understanding,
          such information should be ok if we store them in the Session and use a
          well known key to access them, I'm wondering why you think the Session is
          not appropriate here?



          Regards,
          Walter Wang (wawang@online. microsoft.com, remove 'online.')
          Microsoft Online Community Support

          =============== =============== =============== =====
          When responding to posts, please "Reply to Group" via your newsreader so
          that others may learn and benefit from your issue.
          =============== =============== =============== =====

          This posting is provided "AS IS" with no warranties, and confers no rights.

          Comment

          • Victor

            #6
            Re: customize the httpcontext.cur rent.user

            Hi Walter:
            The reason that I don't use session is because my manager don't want to use
            that. He worries about if there are a lot of user access our website at the
            same time (perhaps 10,000 at about 5mins something like that). It will crash
            the session server. Is there good way to dealing this situation?

            Thanks for the help!
            Victor

            "Walter Wang [MSFT]" <wawang@online. microsoft.comд ÈëÏûÏ¢
            news:Z9Jw9WBfHH A.4692@TK2MSFTN GHUB02.phx.gbl. ..
            Hi Victor,
            >
            I don't understand why the custom IPrincipal object will lose custom
            information, unless the IPrincipal object is not the custom one already. I
            mean: if your class implements IPrincipal and create the instance in
            Application_Aut henticateReques t and assign to Context.User, it should keep
            the reference and you can always cast it to your custom class when used.
            If
            you only see the Name, IsAuthenticated , AuthenticationT ype properties,
            that's because the reference type is IPrincipal. You need to cast it to
            your custom class:
            >
            CustomPrincipal myPrincipal = (CustomPrincipa l) HttpContext.Cur rent.User;
            >
            >
            Above code is about the custom IPrincipal, I understand you're
            implementing
            a custom IIdentity, which is accessed from IPrincipal.Iden tity, which also
            need to cast to your cusom implementation class to access those custom
            information you added.
            >
            >
            Take a step back to look at your question, I understand you only need to
            store some information for the current logined user, I think customizing
            the IPrincipal and IIdentity may not be needed. Based on my understanding,
            such information should be ok if we store them in the Session and use a
            well known key to access them, I'm wondering why you think the Session is
            not appropriate here?
            >
            >
            >
            Regards,
            Walter Wang (wawang@online. microsoft.com, remove 'online.')
            Microsoft Online Community Support
            >
            =============== =============== =============== =====
            When responding to posts, please "Reply to Group" via your newsreader so
            that others may learn and benefit from your issue.
            =============== =============== =============== =====
            >
            This posting is provided "AS IS" with no warranties, and confers no
            rights.
            >

            Comment

            • Walter Wang [MSFT]

              #7
              Re: customize the httpcontext.cur rent.user

              Hi Victor,

              I understand your concern is about the performance maybe affected if you
              store the user information in Session. However, if you want to avoid
              querying database everytime the information is needed, I'm afraid saving it
              in the Session state is really the only way to go.

              Customizing HttpContext.Cur rent requires you set it in every request
              (Application_Au thenticateReque st), which doesn't meet your requirement.

              To avoid 10000 users login in a very short time period, you could use lazy
              loading to load the user information only when needed: you simply create a
              utility method to return a User Information class from Session, and if it's
              not in Session yet, you initialize it from database and store it in
              Session.

              Please reply to let me know your opinion on this suggestion.


              Regards,
              Walter Wang (wawang@online. microsoft.com, remove 'online.')
              Microsoft Online Community Support

              =============== =============== =============== =====
              When responding to posts, please "Reply to Group" via your newsreader so
              that others may learn and benefit from your issue.
              =============== =============== =============== =====

              This posting is provided "AS IS" with no warranties, and confers no rights.

              Comment

              • Walter Wang [MSFT]

                #8
                Re: customize the httpcontext.cur rent.user

                Hi Victor,

                Please let me know if you want to continue work on this issue. You can also
                email me directly. Thanks.


                Regards,
                Walter Wang (wawang@online. microsoft.com, remove 'online.')
                Microsoft Online Community Support

                =============== =============== =============== =====
                When responding to posts, please "Reply to Group" via your newsreader so
                that others may learn and benefit from your issue.
                =============== =============== =============== =====

                This posting is provided "AS IS" with no warranties, and confers no rights.

                Comment

                • Victor

                  #9
                  Re: customize the httpcontext.cur rent.user

                  Hi Walter:
                  Thanks very much for your help. :) I decided to use session for now. I
                  aleardy apply lazy loading in my code.
                  thanks again for your help

                  Victor
                  "Walter Wang [MSFT]" <wawang@online. microsoft.comwr ote in message
                  news:4R0h$CKgHH A.3744@TK2MSFTN GHUB02.phx.gbl. ..
                  Hi Victor,
                  >
                  Please let me know if you want to continue work on this issue. You can
                  also
                  email me directly. Thanks.
                  >
                  >
                  Regards,
                  Walter Wang (wawang@online. microsoft.com, remove 'online.')
                  Microsoft Online Community Support
                  >
                  =============== =============== =============== =====
                  When responding to posts, please "Reply to Group" via your newsreader so
                  that others may learn and benefit from your issue.
                  =============== =============== =============== =====
                  >
                  This posting is provided "AS IS" with no warranties, and confers no
                  rights.
                  >

                  Comment

                  • Walter Wang [MSFT]

                    #10
                    Re: customize the httpcontext.cur rent.user

                    Hi Victor,

                    Thanks for the update.

                    Have a nice day!


                    Regards,
                    Walter Wang (wawang@online. microsoft.com, remove 'online.')
                    Microsoft Online Community Support

                    =============== =============== =============== =====
                    When responding to posts, please "Reply to Group" via your newsreader so
                    that others may learn and benefit from your issue.
                    =============== =============== =============== =====

                    This posting is provided "AS IS" with no warranties, and confers no rights.

                    Comment

                    Working...