How create role to put in the database?

2019-05-31 02:32发布

i wish create a role for each user after that the user authenticate(login) to access to the application i will give some role to and save the role on the database. I will make an example with the database "aspnet.mdf" and Linq toSql to store data but before i need know how create role in c#(WPF) and after created i wish add roles on the database so doing i can assign for each user the right role that i wish . DO you have some idea how do it right??? I will use the database "aspnet.mdf" as example because i see it good just to test my application (i need this feature to develop a project).

Thanks a lot.

Nice Regards,

Bye

2条回答
甜甜的少女心
2楼-- · 2019-05-31 02:35

This is how you might acomplish something like that. I am not sure why you would want to create a role for every user though, kind of defeats the purpose of roles. Anyways something like this will work:

// Check User exists
if (Membership.GetUser("admin") == null)
    Membership.CreateUser("admin", "pass", "admin@domain.com");            

// Check Role exists or create
if (!Roles.RoleExists("AdminRole"))
    Roles.CreateRole("AdminRole");

// Check Users in Roles
if (!Roles.IsUserInRole("admin", "AdminRole"))
    Roles.AddUserToRole("admin", "AdminRole");
查看更多
【Aperson】
3楼-- · 2019-05-31 02:37

You don't.

If you're using ASP.NET's membership framework, you don't touch it using LinqToSql. You use ASP.NET's membership framework.

查看更多
登录 后发表回答