Search code examples
c#asp.netwebformsasp.net-identityasp.net-identity-2

Role-based Security Authorization in web froms using Identity 2.0


I've seen not hundred but THOUSANDS of example where from scratch to complete examples with MVC identity 2.0 are done but not a single one with bloody web forms and the one which are present are not even worth while just very basic. I'm working on an application where I've three roles, user,admin,superUser and all these are in AspNetRoles table because I'm using identity 2.0. now when I create a user I also assign that user one of these roles too. before this role and stuff I've worked on customize roles system as we use to do on desktop applications. so here I tried all the links and articles written on CodeProject about form authentication and all that what we can do in web.config but nothing was helpful Please take a look at this screen shot http://prntscr.com/6ca09i you might get a little idea what I mean by that. My C# code on register is

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //owin entity
        var userStore = new UserStore<IdentityUser>();
        userStore.Context.Database.Connection.ConnectionString =
            System.Configuration.ConfigurationManager
            .ConnectionStrings["GCR"].ConnectionString;

        var manager = new UserManager<IdentityUser>(userStore);
        //string  userInfor;// = new UserInformation();

        // check if the url contains an id perameter
        if (!String.IsNullOrWhiteSpace(Request.QueryString["id"]))
        {
            var id = Convert.ToInt32(Request.QueryString["id"]);
            var userInfo = new UserInformation
            {
                Email = txtEmail.Text,
                FirstName = txtFirstName.Text,
                LastName = txtLastName.Text,
                AddressLine1 = txtAddressLine1.Text,
                AddressLine2 = txtAddressLine2.Text,
                City = txtCity.Text,
                State = ddlState.SelectedValue,
                ZipCode = Convert.ToInt32(txtZip.Text),
                PhoneNumber = txtPhone.Text,
                RoleId = Convert.ToInt32(ddlRole.SelectedValue)

and here is my registration page where I am assigning the roles which are not actually getting assigned http://prntscr.com/6ca1xi

Now please tell me how can I create role based app where in a single folder we have different files which User with different role can get access Please I'd already wasted my two days on Identity I have no wich to waste more time on it


Solution

  • This is how you are going to properly deal with this

            var userInfo = new UserInformation
            {
                Email = txtEmail.Text,
                FirstName = txtFirstName.Text,
                LastName = txtLastName.Text,
                AddressLine1 = txtAddressLine1.Text,
                AddressLine2 = txtAddressLine2.Text,
                City = txtCity.Text,
                State = ddlState.SelectedValue,
                ZipCode = Convert.ToInt32(txtZip.Text),
                PhoneNumber = txtPhone.Text,
                RoleId = ddlRole.SelectedValue
    

    Fist your role should be some text value because it does not take role as id after saving this object or model above `db.saveChanges(); you in the end are going to add this role to aspnetroles table and how you are going to do that is very simple just a single line

     // add role to the user which is created right now 
      manager.AddToRole(userInfo.GUID, ddlRole.Text.Trim());
    

    The first argument is the id of the user and the second one is the dropdown in which you are selecting the user roles and you can also do that role exist stuff in it. now how you are going to check this one page load is very simple and its just like this

    #region page load
                if (!IsPostBack)
                {
                    if (User.IsInRole("admin") || User.IsInRole("superuser"))
                    {
    
                    }
                    else
                    {
                        string unAuthorizedRedirect = WebConfigurationManager.AppSettings["UnAuthorizedRedirect"];
                        Response.Redirect("~/" + unAuthorizedRedirect);
                    }
                }
                #endregion
    

    I hope this helps you completely