Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

New and need a little help

$
0
0

I have started a new web forms project.

I want to simply learn how to implement Identity Authorization and Roles.

I found the Wing Tip Toys example and went straight for the 'Membership and Administration' section.(http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/membership-and-administration)

Everywhere I reference 'userManager.FindByEmail(identityEmail).Id' I get a Null reference Error.

The database is created and the Role is added to the ASPNetRoles Table. The project compiles with no issues. What am I missing? Thanks for your help!

Here is the Role and User Class method I use based off of(Not exactly) the Wing Tip Toys Example:

    internal class RoleActions
    {
        internal void AddUserandRole()
        {
            // Create a database context and member to store returned results
            Models.ApplicationDbContext dbContext = new Models.ApplicationDbContext();
            IdentityResult idRoleResult;
            IdentityResult idUserResult;
            string identityRole = "Administrator";
            string identityEmail = "mike@someEmail.net";
            string identityUserName = "admin";
            string identityUserPWD = "admin";

            // Create the RoleStore object to store Role objects from the database context
            var roleStore = new RoleStore<IdentityRole>(dbContext);

            // Create the RoleManager object to store role objects
            var roleManager = new RoleManager<IdentityRole>(roleStore);

            // Create the Administrator role if it does not exist
            if (!roleManager.RoleExists(identityRole))
            {
                idRoleResult = roleManager.Create(new IdentityRole(identityRole));
            }

            // Create the UserManager object Base on the UserStore and database context
            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(dbContext));

            // Create an application user if it does not exist
            if(userManager.FindByEmail(identityEmail).Id == null)
            {
                var appUser = new ApplicationUser() { UserName = identityUserName, Email = identityEmail };
                idUserResult = userManager.Create(appUser, identityUserPWD);
            }

            //If the User was created add it to the Role
            if(!userManager.IsInRole(userManager.FindByEmail(identityEmail).Id, identityRole))
            {
                idRoleResult = userManager.AddToRole(userManager.FindByEmail(identityEmail).Id, identityRole);
            }

        }



Viewing all articles
Browse latest Browse all 4737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>