Check if user is authenticated

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

    Check if user is authenticated

    Hello,

    How to I check if a user is authenticated and if it is what is its
    role?

    I am using Asp.Net 2.0 and forms authentication.

    Thanks,

    Miguel

  • Peter Bradley

    #2
    Re: Check if user is authenticated

    Use attributes on your classes and/or methods. You need something like
    this:

    [PrincipalPermis sionAttribute(S ecurityAction:: Demand,

    Authenticated=T rue,
    Role="Progdir")]

    The Visual Studio help puts it like this:

    _______________ _______________ _______________ _______________ _______________ ____________
    PrincipalPermis sionAttribute can be used to declaratively demand that users
    running your code belong to a specified role or have been authenticated. Use
    of Unrestricted creates a PrincipalPermis sion with Authenticated set to true
    and Name and Role set to a null reference (Nothing in Visual Basic).

    The scope of the declaration that is allowed depends on the SecurityAction
    that is used. PrincipalPermis sionAttribute cannot be applied at the assembly
    level.

    The security information declared by a security attribute is stored in the
    metadata of the attribute target and is accessed by the system at run time.
    Security attributes are used only for declarative security. For imperative
    security, use the corresponding permission class.

    Important Prior to a demand for principal permission it is necessary to
    set the current application domain's principal policy to the enumeration
    value WindowsPrincipa l. By default, the principal policy is set to
    Unauthenticated Principal. If you do not set the principal policy to
    WindowsPrincipa l, a demand for principal permission will fail. The following
    code should be executed before the principal permission is demanded:
    AppDomain.Curre ntDomain.SetPri ncipalPolicy(Pr incipalPolicy.W indowsPrincipal ).

    Example
    The following example demonstrates how PrincipalPermis sion can be used
    declaratively to demand that the current user is Bob and belongs to the
    Supervisor role.

    Visual Basic Copy Code
    <PrincipalPermi ssionAttribute( SecurityAction. Demand, _
    Name := "Bob", Role := "Supervisor")Pu blic Class SampleClass

    C# Copy Code
    [PrincipalPermis sionAttribute(S ecurityAction.D emand, Name="Bob",
    Role="Superviso r")]

    C++ Copy Code
    [PrincipalPermis sionAttribute(S ecurityAction:: Demand,Name="Bo b",
    Role="Superviso r")]

    J# Copy Code
    /** @attribute PrincipalPermis sionAttribute(S ecurityAction.D emand,
    Name = "Bob", Role = "Supervisor ")
    */


    The following example demonstrates how to demand that the current user's
    identity is Bob, regardless of role membership.

    Visual Basic Copy Code
    <PrincipalPermi ssionAttribute( SecurityAction. Demand, _
    Name := "Bob")Publi c Class SampleClass

    C# Copy Code
    [PrincipalPermis sionAttribute(S ecurityAction.D emand, Name="Bob")]

    C++ Copy Code
    [PrincipalPermis sionAttribute(S ecurityAction:: Demand,Name="Bo b")]

    J# Copy Code
    /** @attribute PrincipalPermis sionAttribute(S ecurityAction.D emand, Name =
    "Bob")
    */


    The following example demonstrates how to demand only that the user is
    authenticated.

    Visual Basic Copy Code
    <PrincipalPermi ssionAttribute( SecurityAction. Demand, _
    Authenticated := True)Public Class SampleClass

    C# Copy Code
    [PrincipalPermis sionAttribute(S ecurityAction.D emand, Authenticated=t rue)]
    _______________ _______________ _______________ _______________ _______________ ___________

    HTH


    Peter


    "shapper" <mdmoura@gmail. comwrote in message
    news:1165510823 .833434.144220@ 16g2000cwy.goog legroups.com...
    Hello,
    >
    How to I check if a user is authenticated and if it is what is its
    role?
    >
    I am using Asp.Net 2.0 and forms authentication.
    >
    Thanks,
    >
    Miguel
    >





    Comment

    • Peter Bradley

      #3
      Re: Check if user is authenticated

      Sorry. Got my syntax screwed up in my first statement. Ignore what I've
      said and stick to what's in the quotation from the help files.


      Peter

      "Peter Bradley" <pbradley@uwic. ac.ukwrote in message
      news:uoIFHUiGHH A.3952@TK2MSFTN GP02.phx.gbl...
      Use attributes on your classes and/or methods. You need something like
      this:
      >
      [PrincipalPermis sionAttribute(S ecurityAction:: Demand,
      >
      Authenticated=T rue,
      Role="Progdir")]
      >
      The Visual Studio help puts it like this:
      >
      _______________ _______________ _______________ _______________ _______________ ____________
      PrincipalPermis sionAttribute can be used to declaratively demand that
      users running your code belong to a specified role or have been
      authenticated. Use of Unrestricted creates a PrincipalPermis sion with
      Authenticated set to true and Name and Role set to a null reference
      (Nothing in Visual Basic).
      >
      The scope of the declaration that is allowed depends on the SecurityAction
      that is used. PrincipalPermis sionAttribute cannot be applied at the
      assembly level.
      >
      The security information declared by a security attribute is stored in the
      metadata of the attribute target and is accessed by the system at run
      time. Security attributes are used only for declarative security. For
      imperative security, use the corresponding permission class.
      >
      Important Prior to a demand for principal permission it is necessary to
      set the current application domain's principal policy to the enumeration
      value WindowsPrincipa l. By default, the principal policy is set to
      Unauthenticated Principal. If you do not set the principal policy to
      WindowsPrincipa l, a demand for principal permission will fail. The
      following code should be executed before the principal permission is
      demanded:
      AppDomain.Curre ntDomain.SetPri ncipalPolicy(Pr incipalPolicy.W indowsPrincipal ).
      >
      Example
      The following example demonstrates how PrincipalPermis sion can be used
      declaratively to demand that the current user is Bob and belongs to the
      Supervisor role.
      >
      Visual Basic Copy Code
      <PrincipalPermi ssionAttribute( SecurityAction. Demand, _
      Name := "Bob", Role := "Supervisor")Pu blic Class SampleClass
      >
      C# Copy Code
      [PrincipalPermis sionAttribute(S ecurityAction.D emand, Name="Bob",
      Role="Superviso r")]
      >
      C++ Copy Code
      [PrincipalPermis sionAttribute(S ecurityAction:: Demand,Name="Bo b",
      Role="Superviso r")]
      >
      J# Copy Code
      /** @attribute PrincipalPermis sionAttribute(S ecurityAction.D emand,
      Name = "Bob", Role = "Supervisor ")
      */
      >
      >
      The following example demonstrates how to demand that the current user's
      identity is Bob, regardless of role membership.
      >
      Visual Basic Copy Code
      <PrincipalPermi ssionAttribute( SecurityAction. Demand, _
      Name := "Bob")Publi c Class SampleClass
      >
      C# Copy Code
      [PrincipalPermis sionAttribute(S ecurityAction.D emand, Name="Bob")]
      >
      C++ Copy Code
      [PrincipalPermis sionAttribute(S ecurityAction:: Demand,Name="Bo b")]
      >
      J# Copy Code
      /** @attribute PrincipalPermis sionAttribute(S ecurityAction.D emand, Name =
      "Bob")
      */
      >
      >
      The following example demonstrates how to demand only that the user is
      authenticated.
      >
      Visual Basic Copy Code
      <PrincipalPermi ssionAttribute( SecurityAction. Demand, _
      Authenticated := True)Public Class SampleClass
      >
      C# Copy Code
      [PrincipalPermis sionAttribute(S ecurityAction.D emand, Authenticated=t rue)]
      _______________ _______________ _______________ _______________ _______________ ___________
      >
      HTH
      >
      >
      Peter
      >
      >
      "shapper" <mdmoura@gmail. comwrote in message
      news:1165510823 .833434.144220@ 16g2000cwy.goog legroups.com...
      >Hello,
      >>
      >How to I check if a user is authenticated and if it is what is its
      >role?
      >>
      >I am using Asp.Net 2.0 and forms authentication.
      >>
      >Thanks,
      >>
      >Miguel
      >>
      >
      >
      >

      Comment

      • Patrick.O.Ige

        #4
        Re: Check if user is authenticated

        I taught as much that would do the trick also as Peter adviced.
        Patrick

        "Peter Bromberg [C# MVP]" <pbromberg@yaho o.nospammin.com wrote in message
        news:08A202B3-005B-434B-ABA4-2E0756A2F5FF@mi crosoft.com...
        try User.IsAuthenti cated, and User.IsInRoke(" rolename").
        Depending on whether you are using Membership, there are a lot of other
        built-in methods.
        Peter
        >
        --
        Co-founder, Eggheadcafe.com developer portal:

        UnBlog:

        >
        >
        >
        >
        "shapper" wrote:
        >
        Hello,

        How to I check if a user is authenticated and if it is what is its
        role?

        I am using Asp.Net 2.0 and forms authentication.

        Thanks,

        Miguel

        Comment

        Working...