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

Else

  " 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" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Welcome page</title>

</head> <body>

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

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

       </form>

</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

Else

  " 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>