ASP.NET Tutorial/Authentication Authorization/User

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Checking whether the user is authenticated

   <source lang="csharp">

Dim AuthUser As Boolean AuthUser = User.Identity.IsAuthenticated()</source>

Checking whether the user is part of a specific role

   <source lang="csharp">

If (User.IsInRole("ReutersServer\Subscribers")) Then

  " Private information for subscribers


  " Public information

End If</source>

Getting the username of the logged-in user

   <source lang="csharp">

Dim UserName As String UserName = User.Identity.Name</source>

Output the name of the currently logged-on user as authenticated by the ASP.NET application

   <source lang="csharp">

<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" ""> <html xmlns="" > <head runat="server">

   <title>Welcome page</title>

</head> <body>

       <form id="form1" runat="server">

Welcome, <%=User.Identity.Name %>


</body> </html></source>

Using an If/Then statement that checks authentication

   <source lang="csharp">

If (User.Identity.IsAuthenticated()) Then

  " Do some actions here for authenticated users


  " Do other actions here for unauthenticated users

End If</source>

Using the AuthenticationType property

   <source lang="csharp">

Dim AuthType As String AuthType = User.Identity.AuthenticationType</source>

Using the WindowsBuiltInRole enumeration

   <source lang="csharp">

Dim AdminUser As Boolean AdminUser = User.IsInRole(WindowsBuiltInRole.Administrator)</source>