473,657 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Dec 7 '06 #1
3 4591
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" <md*****@gmail. comwrote in message
news:11******** **************@ 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



Dec 7 '06 #2
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" <pb******@uwic. ac.ukwrote in message
news:uo******** ******@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" <md*****@gmail. comwrote in message
news:11******** **************@ 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



Dec 7 '06 #3
I taught as much that would do the trick also as Peter adviced.
Patrick

"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com wrote in message
news:08******** *************** ***********@mic rosoft.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:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"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

Dec 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
11097
by: Georg Vassilopulos | last post by:
Hello! How do I check user input for following formats: I only want to accept floats or integers written as: 232,45 232 242.45 345
14
42399
by: deko | last post by:
Is there a way to check user input for illegal characters? For example, a user enters something into a text box and clicks OK. At that point I'd like to run code such as this: illegal = Array(\, /, :, *, ?, ", <, >, |) If Me.TextBox Contains illegal Then MsgBox "You entered illegal characters. Please try again." Me.TextBox = Null
4
2691
by: ad | last post by:
I am using MSDE as the database of my program. At the begin of my program, it must display a form for user to enter the password After the user enters the password, I will check the password, if it is the correct password of a user of MSDE ..
1
1799
by: Grey | last post by:
my web server is in domain. how can I check the user login with the AD's user account?? does it required the client's browser and web server located in the same domain?? and what is the different between using AD and LDAP?? Million Thanks.
1
2200
by: sincethe2004 | last post by:
By using <authentication mode="Forms" > in web.config, we can create self-designed login page, but how to check user's account and password is vaild in another domain controller? Does <authentication mode="Windows"> can have self-designed login page?
1
2084
by: Wijit Sattayaporn | last post by:
All, I would like to develop intranet with ASP.Net as creating web form for authentication user before. Now I have text box for input "User Name", "Password" and drop down list for selecting "Domain". If I want to check that "User Name" and "Password" could have in "Domain" or not, how to????? P.S. : I don't have Active Directory, just Domain Control.
2
1717
by: Jeroen | last post by:
I am developing an asp.net website with windows authentification and I want to check to which group the user belongs. I know how to check for the user that has logged in and to see if he belongs to a group that was predefined by windows but I need to know all the groups he belongs to (also the ones that I created myself). Please help me with this, I'm going crazy ! Thanks,
5
4166
by: Shimon Sim | last post by:
I am working with LogIn control and need to set some property for profile after user is authenticated. I thought that LoggedIn event is the right place but system tells me that the property can't be set for anonymous user. I checked User.Identity.IsAuthenticated = false. So when is the good place to set profile properties? Again I need to set up it only once right after user logged in. Thank you.
3
16260
by: shapper | last post by:
Hello, I need to check if a user is authenticated. I tried everything I could think and find: If Membership.ValidateUser("shapper", "27lamps11") Then Response.Write(Request.IsAuthenticated.ToString) Response.Write(My.User.IsAuthenticated.ToString) Response.Write(Me.User.Identity.IsAuthenticated.ToString) Else
4
1437
by: shapper | last post by:
Hello, I am creating a Poll system and I need to check if a user has already voted. What should be the best way to do this? 1. Should I save the user IP along with its vote in the database? But does not some users IP change each time they access the internet?
0
8394
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4152
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.