web.config
codice:<system.web> <compilation debug="true" targetFramework="4.0"/> <customErrors mode="Off"/> <authentication mode="Forms"> <forms loginUrl="Login.aspx" name=".ASPXFORMSAUTH"/> </authentication> </system.web>
global.asax
fra i vari eventi assicurati che ci sia questo:
codice:protected void Application_AuthenticateRequest(object sender, EventArgs e) { if (Context.Request.IsAuthenticated) { // // CERCA L'UTENTE COLLEGATO NEL DB PER RECUPERARE IL RUOLO // if (!string.IsNullOrEmpty(User.Identity.Name.ToString())) { DatabaseLibrary db = new DatabaseLibrary(); db.SQL = "SELECT [ruolo] FROM TB_UTENTI WHERE NOMEUTENTE='" + User.Identity.Name.ToString() + "'"; DataTable dt = db.GetDataTable("verificaRuoloUtente"); if (dt != null && dt.Rows.Count > 0) { // // ASSEGNA IL RUOLO O I RUOLI LETTI ALL'UTENTE CORRENTE // string[] ruoli = dt.Rows[0]["ruolo"].ToString().Trim().Split(','); System.Web.HttpContext.Current.User = new GenericPrincipal(System.Web.HttpContext.Current.User.Identity, ruoli); } } } }
login.aspx
(ho usato il componente login del Fw)
codice:<asp:Login RememberMeText="Memorizza login&nbsp;&nbsp;" LoginButtonText="ENTRA" VisibleWhenLoggedIn="false" UserNameLabelText="Nome Utente&nbsp;&nbsp;" PasswordLabelText="&nbsp;Password&nbsp;" ID="usr_login" runat="server" Height="115px" Orientation="Horizontal" PasswordRequiredErrorMessage="Password obbligatoria." TitleText="Accesso al portale<br/><br/>" UserNameRequiredErrorMessage="Nome utente obbligatorio." OnAuthenticate="usr_login_Authenticate" FailureText="Utente non riconosciuto. Riprovare."> <TextBoxStyle Width="170px" Height="15" /> <TitleTextStyle BackColor="DodgerBlue" Font-Size="Medium" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False" /> <LabelStyle Font-Size="Small" /><ValidatorTextStyle Font-Size="Small" /><CheckBoxStyle Font-Size="Small" /> </asp:Login>
login.aspx.cs
codice:protected void usr_login_Authenticate(object sender, AuthenticateEventArgs e) { DatabaseLibrary db = new DatabaseLibrary(); db.SQL = "SELECT [NOMEUTENTE],[PASSWORD] FROM TB_UTENTI WHERE NOMEUTENTE='" + usr_login.UserName + "' AND PASSWORD='" + usr_login.Password + "' AND ATTIVO = 1"; DataTable dt = db.GetDataTable("verificaUtente"); if (dt != null && dt.Rows.Count > 0 && dt.Rows[0]["NOMEUTENTE"].ToString().ToUpper() == usr_login.UserName.ToUpper() && dt.Rows[0]["PASSWORD"].ToString().ToUpper() == usr_login.Password.ToUpper()) { FormsAuthentication.RedirectFromLoginPage(usr_login.UserName, true); } }
la libreria databaselibrary e' solo un wrapper per il DB, puoi accedere al tuo DB dove c'e' la tabella utenti e verificare User e PW.
il flusso poi sara' a carico dell'evento del global.asax, che gli assegnera' il ruolo.
Nella pagina di Default poi ho messo, semplicemente (....la gestione dell'accesso alle cartelle te la posto dopo, e' solo una impostazione di un ulteriore <web.config> interno alla cartella)
Default.aspx.cs
codice:protected void Page_Load(object sender, EventArgs e) { lbUser.Text = User.Identity.Name == string.Empty ? "Salve, Utente." : "Salve, " + User.Identity.Name; if (User.IsInRole("admin")) // abilita menu' Admin else // abilita menu' Utente }

Rispondi quotando