Abdullah Wajahat
Abdullah Wajahat

Reputation: 83

Maintaining a Session in MVC .net application using angularjs

I am working on an application, in which a user if has an account in db can log in the website and then perform certain functions. One of those functions involve creating a blog. The blog is being displayed in another project application using the same database. Now when user creates a blog after logging in, i need to store who created the blog in order to do that, i came up with 2 ways. Either i keep passing the user id as a parameter on every page url or i can create a session in order to store it for the duration of login.

I think the latter is a better option but i am kind of lost on how to do it. I am creating a 3 project layer Application. with the client side done in angularjs. My c# controller is being used just to pass the json data to another layer, which then communicates with the database which is in another layer.

The project files are too big but i can write a example code for it.

Html:

    <div ng-app="Module">
       <div ng-controller="AppController">
          <input ng-model="user.Email" type="email"\>
          <button type="button" ng-click="UserLogin()"\>
       </div>
    </div>

AngualrJs:

    var app = angular.module('Module', []);
    app.controller("AppController", function ($scope) {

          $scope.loginchk = function () {

    Post("/User/LoginValidation", $scope.user, false, $("#btnlogin")).then(function (d) {
        if (d.Success) {

            window.location.href = "/User/LoggedIn?emailId=" + $scope.user.Email;
        }
        ShowMessage(d);

    });
}               

    })

Controller:

     public JsonResult LoginValidation(LoginUser user) {
        return Json((new userLogic()).LoginChk(user), JsonRequestBehavior.AllowGet);
    }

Business Logic LAYER----------------

UserLogic:

     public Message LoginChk(LoginUser user) {
        Message msg = new Message();
        try {
            Account userProfile = db.Accounts.Where(b => b.Email == user.Email).FirstOrDefault();
            if (userProfile == null)
            {
                msg.Success = false;
                msg.MessageDetail = "Account Email does not exist";

            }
            else
            {
                if (userProfile.password != user.Password)
                {
                    msg.Success = false;
                    msg.MessageDetail = "Wrong Password";
                }
                else
                {
                    msg.Success = true;
                    msg.MessageDetail = "Logged In";

                }

            }
        }
        catch(Exception ex)
        {

            msg.Success = false;
            msg.MessageDetail = "DB connection failed.";
        }

        return msg;
    }

Now I know i can create a Session Variable in the controller like this Session['Sessionname'] = user; but i am not sure it will work with my application because i have multiple controllers and i will still have to pass it to them. so i dont see the point of maintaining a session variable in every controller even if its not used. How do i go about creating a session?

Upvotes: 0

Views: 667

Answers (2)

Majid Khakwani
Majid Khakwani

Reputation: 26

 local storage is best option to do that :
window.localStorage.setItem("userId",useId);
to get again:
localStorage.getItem("userId");

Upvotes: 1

Arvind Maurya
Arvind Maurya

Reputation: 928

You Can use client-side LocalStorage to save the user-id and use it where ever necessary, as it will be saved in plain text you can encrypt and save it . check here how to encrypt using javascript https://stackoverflow.com/a/40478682/7262120

Upvotes: 1

Related Questions