Mega Code Archive

 
Categories / ASP.Net Tutorial / Authentication Authorization
 

Windows roles

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"  Inherits="_Default" %> <!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>Untitled Page</title> </head> <body>     <form id="form1" runat="server">     <div>         <asp:Label ID="LabelPrincipalInfo" runat="server" />     </div>     </form> </body> </html> File: Web.config <?xml version="1.0"?> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">     <system.web>         <compilation debug="true"/>         <authentication mode="Windows"/>         <authorization>             <deny users="?" />         </authorization>         <roleManager enabled="true"                      cacheRolesInCookie="false"                      defaultProvider="WindowsRoles">             <providers>                 <add name="WindowsRoles"                      type="System.Web.Security.WindowsTokenRoleProvider" />             </providers>         </roleManager>     </system.web> </configuration> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; public partial class _Default : System.Web.UI.Page  {     protected void Page_Load(object sender, EventArgs e)     {         if ((User != null) && (User.Identity.IsAuthenticated))         {             RolePrincipal rp = (RolePrincipal)User;             StringBuilder Info = new StringBuilder();             Info.AppendFormat("<h2>Welcome {0}!</h2>", User.Identity.Name);             Info.AppendFormat("<b>Provider: </b>{0}<br>", rp.ProviderName);             Info.AppendFormat("<b>Version: </b>{0}<br>", rp.Version);             Info.AppendFormat("<b>Expiration: </b>{0}<br>", rp.ExpireDate);             Info.AppendFormat("<b>Roles: </b><br>");             string[] Roles = rp.GetRoles();             foreach (string role in Roles)             {                 if (!role.Equals(string.Empty))                     Info.AppendFormat("-) {0}<br>", role);             }             LabelPrincipalInfo.Text = Info.ToString();         }     } }