问题
I am trying to find a good walkthrough or example of how to use the new Identity authorization system with added roles. When you create a new website in VS 2013 there is also an Account folder and in the database you have the tables also connected to roles. But all examples available are connected to MVC! Does anybody have a link to a good Identity users or programmers guide that does not use MVC?
Looking forward to any proposal in this matter.
回答1:
Here is an excellent step-by-step significant informative tutorial as part of asp.net/webforms learning path.
It gives thorough knowledge & details like,
- Create/Manage Roles
- Assigning Roles to Users
- Role-Based Configuration
Update: Here is Asp.net Identity tutorial for web forms for empty project & existing web-forms. For roles customization, you can refer this article. Though it is in MVC 5 but it applies to asp.net web-forms as well.
回答2:
I am attempting to do the same exact thing. This is the tutorial that I'm using to help accomplish creating a role using web forms.
It branches off a previous project, but it gives a download link to the previous project to build upon. Hope this helps!
The only downside is it creates a brand new user and adds it into the new role instead of assigning a current user to it.
http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/membership-and-administration
回答3:
I've found an answer on another page here to add users to roles by Tarzan. Here is the code:
internal class Security
{
ApplicationDbContext context = new ApplicationDbContext();
internal void AddUserToRole(string userName, string roleName)
{
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
try
{
var user = UserManager.FindByName(userName);
UserManager.AddToRole(user.Id, roleName);
context.SaveChanges();
}
catch
{
throw;
}
}
}
来源:https://stackoverflow.com/questions/23297712/using-identity-and-roles-in-web-forms-in-asp-net-4-5-1