locked
Using web service to register/login users RRS feed

  • Question

  • User419286389 posted

    Hi,

        I want to allow users to register their details either using an MVC web site or by using a Windows Phone app (which in turn uses a web service). Whichever method the user chooses the data should be stored in the same database. The standard MVC web project gives me this functionality for the web site but I can't figure out what I need to add in the 'RegisterUser' function of the web service can anyone help?

    Here is the web service function:

    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.Owin;
    using MVC.Models;

    namespace MVC
    {
        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
        // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
        public class Service1 : IService1
        {
            public int RegisterUser(RegisterViewModel regUser)
            {
                // How do you register the user here???

                return 0;
            }
        }
    }

    To re-create this scenario just create a new MVC web project in VS2013 and then add a new 'Web Service' item and create the function listed above.

    SteveR

    Tuesday, September 2, 2014 3:57 PM

Answers

  • User419286389 posted

    Hi Greg,

        I tried that but the compiler says I have to change the function definition to async Task<int> in the meantime I found another solution that works perfectly and doesn't need this change, here is the code for it.

            public int RegisterUser(RegisterViewModel regUser)
            {
                int iRet = 999;
                MVC.Models.ApplicationDbContext context = MVC.Models.ApplicationDbContext.Create();
    
                IdentityResult ir;
                
                var um = new UserManager<ApplicationUser>(
                    new UserStore<ApplicationUser>(context));
    
                var user = new ApplicationUser()
                {
                    UserName = regUser.Email,
                    Email = regUser.Email,
                };
    
                ir = um.Create(user, regUser.Password);
                if (ir.Succeeded == true)
                    iRet = 0;
    
                return iRet;
            }

    Now that's working I need to get the SignIn and SignOut functions working - any suggestions?

    SteveR
    ,

        I tried that but the compiler says I have to change the function definition to async Task<int> but I found another way to do it that doesn't require this change, here is the code:

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 3, 2014 4:58 PM

All replies

  • User103196646 posted

    Hello Richson64! Thanks for your post!

    Just take the same code that's in the out of the box MVC controller - AccountController.cs - and paste it into your web service method:

        public class Service1 : IService1
        {
            public int RegisterUser(RegisterViewModel model)
            {
                // Here's how to register the user
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };

    var result = await UserManager.CreateAsync(user, model.Password);

    if (result.Succeeded)
    {
      return 1;
    }
    else return 0; } }

    Regards!

    Tuesday, September 2, 2014 4:26 PM
  • User419286389 posted

    Hi Greg,

        I tried that but the compiler says I have to change the function definition to async Task<int> in the meantime I found another solution that works perfectly and doesn't need this change, here is the code for it.

            public int RegisterUser(RegisterViewModel regUser)
            {
                int iRet = 999;
                MVC.Models.ApplicationDbContext context = MVC.Models.ApplicationDbContext.Create();
    
                IdentityResult ir;
                
                var um = new UserManager<ApplicationUser>(
                    new UserStore<ApplicationUser>(context));
    
                var user = new ApplicationUser()
                {
                    UserName = regUser.Email,
                    Email = regUser.Email,
                };
    
                ir = um.Create(user, regUser.Password);
                if (ir.Succeeded == true)
                    iRet = 0;
    
                return iRet;
            }

    Now that's working I need to get the SignIn and SignOut functions working - any suggestions?

    SteveR
    ,

        I tried that but the compiler says I have to change the function definition to async Task<int> but I found another way to do it that doesn't require this change, here is the code:

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 3, 2014 4:58 PM