Pergunta

I'm having trouble understanding how the Role mechanism works with ASP.NET.

I have a Microsoft SQL Server database where everything is already set up and I don't want to add/remove/modify the tables. There is a field in the "Users" table that specify the "Role" of the user by an ID referencing an entry in the "Types" table. I want to use this field to differentiate the users by their types (which could be administrator, premium or standard for example).

Isn't it possible to assign a role to the user each time he connects?

I mean something like this in the Login.aspx.cs:

protected void btn_login_Click(object sender, EventArgs e)
{
    // On authentifie l'utilisateur via la BLL
    BusinessLogicLayer bll = new BusinessLogicLayer();

    Utilisateur user = bll.authenticate(txt_login.Text, txt_password.Text);

    // SUCCEEDED AUTH
    if (user != null)
    {
        // Ajout de l'utilisateur à son role correspondant
        if (user.Type1.nom == "lecteur")
            Roles.AddUserToRole(user.login, "lecteur");

        else if (user.Type1.nom == "journaliste")
            Roles.AddUserToRole(user.login, "journaliste");

        else if (user.Type1.nom == "administrateur")
            Roles.AddUserToRole(user.login, "administrateur");

        FormsAuthentication.RedirectFromLoginPage(user.login, cb_remember.Checked);
    }

    // FAILED AUTH (si on arrive jusqu'ici vu qu'on a pas été redirigé)
    else
        lbl_invalidCredentials.Visible = true;
}

I have already read the tutorial on the ASP.NET website but it uses a specific database configuration that I cannot implement.

Foi útil?

Solução

Role manager uses of the ASPDBNET.mdf (membership) by default.

if you want create custom role provider try this:

How to: Sample Role-Provider Implementation

Update:
you can use Session instead of role manager:

in login page:

Session["role"] = "lecteur" ;

in admin page:

if(Session["role"]!=null)
{
    if(Session["role"]=="lecteur")
    { 
        //welcome admin
    }
    else
    {
        //access denied
    }
}
else
{
    //access denied
}  

Update

or you can use role provider into linq

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top