Windows roles : Role « Authentication Authorization « ASP.NET Tutorial






<%@ 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();
        }
    }
}








21.14.Role
21.14.1.Getting all the roles of a specific user
21.14.2.Enables the SqlRoleProvider.
21.14.3.Assigning a new user to a role.
21.14.4.Configuring the WindowsTokenRoleProvider
21.14.5.Displaying different content to members of the Windows Administrators group.
21.14.6.Displaying a user's roles.
21.14.7.Windows roles
21.14.8.Adding roles to the application
21.14.9.Looking up users in a particular role