locked
Identity - returning custom user properties in custom method RRS feed

  • Question

  • User-570626059 posted

    I find myself in several place updating the user.webstatus field so I would like to create my own function to do this in a class file so I can just call this instead.

    Here is my current code

    case SignInStatus.Success:
    
                            var finduserid = manager.FindByName(textUsername.Value).Id;
    
                            var isadmin = manager.IsInRole(finduserid, "Admin");
    
                            var currentUser = manager.FindById(finduserid);
    
                            if (isadmin)
                            {
                                Response.Redirect("~/pages/admin/adminland.aspx");
                            }
                            else
                            {
                                // check for first login
                                var empty = UtilsString.IsEmpty(currentUser.firstLogin.Trim().ToUpper());
    
                                if (!empty)
                                {
                                    if (currentUser.firstLogin.Trim().ToUpper() == "Y")
                                    {
    
                                        currentUser.webStatus = "Live";
    
                                        manager.Update(currentUser);
    
                                        Response.Redirect("~/account/managepassword.aspx");
                                    }
                                    else
                                    {
                                        Response.Redirect("~/pages/userland.aspx");
    
                                    }
                                }
    
                            }

    II have created this method. But I cant create Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); in the class file so I am a bit confused as to how this is done correctly.

      ///<method>
            ///Update the users webstatus field, pass userid and numeric
            /// 1 - Live
            /// 2 - Logged In
            /// 3 - Invitation Sent
            ///<method>
            public IdentityResult SetUserWebstatus(string userID,int Status,ApplicationUserManager applicationUserManager)
            {
                string statusname = "";
              
                var user = applicationUserManager.FindById(userID);
    
                switch (Status)
                {
                    case 1:
                        statusname = "Live";
    
                        break;
                    case 2:
                        statusname = "Logged In";
                        break;
    
                    case 3:
    
                        statusname = "Invitation Sent";
    
                        break;
    
                    default:
                        statusname = "";
                        break;
                }
    
                user.webStatus = statusname;
    
                var result=applicationUserManager.Update(user);
                return result;
            }
    

    Please can you help point me in the right direction so I can do this correctly.

    Wednesday, July 4, 2018 2:49 PM

All replies

  • User475983607 posted

    Seems like a design issue.  Perhaps the status should be a claim that drives security or code access.  Or maybe use an enumeration since the code converts an int to a string constant which is what an enumeration is for.

    Wednesday, July 4, 2018 3:00 PM