Login and SignUP Services in ASP.NET Web API - Lesson 2

How to Create Login and SignUP Restful Web Services using Web API and Store Credentials in List


Category: ASP.NET Tags: ASP.NET, WebApi, C#, MVC


Introduction:

        Login and Signup are two basic things come in your mind when you start building any application which provide facility of membership to users. In this article I am going to show you a way to create Login and Signup Restful web services using Web API.

Creating Models:

Right click on Models Folder, add a new class called Users and paste this code
namespace Learning.Models
{
    public class Users
    {
        public string Name { get; set; }
        public string UName { get; set; }
        public string Password { get; set; }
    }
    public class UserLogin
    {
        public string UName { get; set; }
        public string Password { get; set; }
    }
    public class StatusClass
    {
        public string Status { get; set; }
        public bool HasError { get; set; }
        public bool IsLoggedIn { get; set; }
        public string Name { get; set; }
    }
}

Users List:

Generally we use database to store users data. But for shorting this I will show you how to store users in a list. Create a class called UsersList in Models folder Which will look like following:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Learning.Models
{
    public class UsersList
    {
        public static List<Users> users = new List<Users>();

        public static bool AddUser(Users user)
        {
            if (!string.IsNullOrEmpty(user.UName) && !string.IsNullOrEmpty(user.Name) && !string.IsNullOrEmpty(user.Password))
            {
                bool IsAvb = users.Any(u => u.UName == user.UName);

                if (!IsAvb)
                {
                    lock (users)
                    {
                        users.Add(user);
                    }
                    return true;
                }
                else
                {
                    throw new Exception("UserName is Already Exist");
                }
            }
            else
            {
                throw new Exception("Fill All User Details");
            }
        }

        private UsersList usersList = new UsersList();
    }
}

Here we created a singleton class having a list of users and a method “AddUser” for signup. Here we are checking if any of field shouldn’t be empty or null. And “IsAvb” will return true if any UserName (UName) is already exist and false if not exist because we cannot have duplicate UserNames.

Controller:

Now right click on controller and add a controller called AccountController, Will look like:
using System;
using System.Linq;
using System.Web.Http;
using Learning.Models;

namespace Learning.Controllers
{
    public class AccountController : ApiController
    {
    }
}
And add a SignUp Method:
        [HttpPost]
        public StatusClass SignUP(Users user)
        {
            StatusClass status = new StatusClass();
            try
            {
                if (!string.IsNullOrEmpty(user.UName) && !string.IsNullOrEmpty(user.Password) && !string.IsNullOrEmpty(user.Name))
                {
                    if (UsersList.AddUser(user))
                    {
                        status.HasError = false;
                        status.IsLoggedIn = true;
                        status.Status = "ok";
                        status.Name = user.Name;
                        return status;
                    }
                    else
                    {
                        status.HasError = false;
                        status.IsLoggedIn = false;
                        status.Status = "Something is wrong";
                        return status;
                    }
                }
                else
                {
                    status.HasError = false;
                    status.IsLoggedIn = false;
                    status.Status = "Please Provide All Details";
                    return status;
                }
            }
            catch(Exception obj)
            {
                status.HasError = true;
                status.IsLoggedIn = false;
                status.Status = obj.Message;
                return status;
            }
        }

Here we are just adding New user using AddUser Method of UsersList class. If UserName is exist then it will throw an Exception: UserName is Already Exist

Now add a Login Method:
        [HttpPost]
        public StatusClass Login(UserLogin userData)
        {
            StatusClass status = new StatusClass();
            try
            {
                if (!string.IsNullOrEmpty(userData.UName) && !string.IsNullOrEmpty(userData.Password))
                {
                    var fetchedUser = UsersList.users.Where(u => u.UName == userData.UName && u.Password == userData.Password);
                    if (fetchedUser.Count() > 0)
                    {
                        status.HasError = false;
                        status.IsLoggedIn = true;
                        status.Status = "ok";
                        status.Name = fetchedUser.First().Name;
                        return status;
                    }
                    else
                    {
                        status.HasError = false;
                        status.IsLoggedIn = false;
                        status.Status = "Id or Password Incorrect";
                        return status;
                    }
                }
                else
                {
                    status.HasError = false;
                    status.IsLoggedIn = false;
                    status.Status = "Please Provide All Details";
                    return status;
                }
            }
            catch
            {
                status.HasError = true;
                status.IsLoggedIn = false;
                status.Status = "error";
                return status;
            }
        }
In above code we will fetch the user of matching UName and Password. If count comes more than zero we return IsLoggin in status is "true" and status is "ok".

Test on RestClient:

You just can add Advance REST Client plugin to your google chrome or firefox and would be look like:

  
Here you can see the link and post data will look like:
SignUP:
Link:
https://localhost:1620/api/Account/SignUP
Post Data:
{
"Name":"Nikhil",
"UName": "nikhil123",
"Password": "123"
}
And response will be:
{
Status: "ok"
HasError: false
IsLoggedIn: true
Name: "Nikhil"
}
Login:
Link:
https://localhost:1620/api/Account/Login
Post Data:
{
"UName": "nikhil123",
"Password": "123"
}
And response will be:
{
Status: "ok"
HasError: false
IsLoggedIn: true
Name: "Nikhil"
}

So this was how to store users credentials in list, Register users and provide Login. Here we are using status class for maintain additional information like HasError, Status and IsLoggedIn


Like 1 Person
Last modified on 11 October 2018
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 135
Questions: 9
Given Best Solutions: 9 *

Comments:


You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x