Using Identity and roles in web-forms in asp.net 4.5.1

前提是你 提交于 2021-02-07 14:31:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!