AWS User Authorization and get the necessary information from your organization in ASP.NET MVC


In this article, I will explain how to Authorize users to AWS web server and pull the necessary information from your organization like user name, email, and photo  using ASP.NET MVC

Below code, you need to create in the App_Start folder as a class file.

C#


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using opentoken;
using opentoken.util;


namespace ASPShortCodes.App_Start
{
    public class PAuthorize: AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            bool isAuth = isAuthorised(httpContext);
          
            return isAuth;
        }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (!AuthorizeCore(filterContext.HttpContext))
            {
                HandleUnauthorizedRequest(filterContext);
            }
        }
        private bool isAuthorised(HttpContextBase httpContext)
        {
            bool retValue = false;
            HttpContext context = HttpContext.Current;

            string Currentpath = "/Home/Index";

            if (HttpContext.Current.Request.Url.AbsolutePath != null)
            {
                Currentpath = HttpContext.Current.Request.Url.AbsolutePath;
            }

           

            if (context.Session["FirstTimeFlag"] == null)// && Currentpath != null && Currentpath != "/" && Currentpath != "/Home/Index")
            {
                context.Session["FirstTimeFlag"] = HttpContext.Current.Request.Url.AbsolutePath;//"True";
                context.Session["PreURL"] = HttpContext.Current.Request.Url.AbsoluteUri;
            }




            ////manual run
            if (HttpContext.Current.Request.Url.Host == "localhost" || HttpContext.Current.Request.Url.Host == "userid")
            {
                context.Session.Add("USER_ID", "userid");
                context.Session.Add("UserEmail", "UserEmail.com");
                context.Session.Add("UserName", "UserName");
                context.Session.Add("userGivenName", "userGivenName");
                context.Session.Add("LastName", "LastName");


                GetMyImage myIMG = new GetMyImage();
                var imgr = myIMG.GetUserImage(ConfigurationManager.AppSettings["ClinetId"].ToString(), ConfigurationManager.AppSettings["ClientSecret"].ToString(), "shabbeer.mujawar@honeywell.com");
                if (imgr.Length > 0)
                    context.Session.Add("UserImg", string.Format("data:image/png;base64,{0}", Convert.ToBase64String(imgr)));
                else
                    context.Session.Add("UserImg", "/Content/img/user.png");
                return true;

            }
            else
            {

                try
                {
                    if (context.Session["USER_ID"] != null)
                    {
                        retValue = true;
                    }
                    else
                    {
                        Agent agent = new Agent(HttpContext.Current.Server.MapPath("~/agent-config.txt"));
                        MultiStringDictionary userInfo = agent.ReadTokenMultiStringDictionary(HttpContext.Current.Request);
                        if (userInfo != null)
                        {
                            string usernae = userInfo[Agent.TOKEN_SUBJECT][0];
                            context.Session.Add("USER_ID", userInfo[Agent.TOKEN_SUBJECT][0]);
                            context.Session.Add("UserEmail", userInfo["email"][0]);
                            context.Session.Add("UserName", userInfo["FirstName"][0] + " " + userInfo["LastName"][0]);
                            context.Session.Add("userGivenName", userInfo["FirstName"][0]);
                            context.Session.Add("LastName", userInfo["LastName"][0]);



                            GetMyImage myIMG = new GetMyImage();
                            //var imgr =myIMG.GetUserImage("dbe96fd4-c914-4167-af84-22993152b53b", "ylrzNRS97}}ddrJTDP187{;", userInfo["email"][0].ToString());
                            var imgr = myIMG.GetUserImage(ConfigurationManager.AppSettings["ClinetId"].ToString(), ConfigurationManager.AppSettings["ClientSecret"].ToString(), userInfo["email"][0].ToString());
                            if (imgr.Length > 0)
                                context.Session.Add("UserImg", string.Format("data:image/png;base64,{0}", Convert.ToBase64String(imgr)));
                            else
                                context.Session.Add("UserImg", "/Content/img/user.png");




                            retValue = true;
                        }
                    }
                }
                catch (TokenException e)
                {
                    retValue = false;
                    string a = e.Message;
                }
                return retValue;

            }

        }

        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {

            filterContext.Result = new RedirectResult("/Home/Login");

           

        }



    }
}





Please comment if you need any other information.


Thank You
Shabbeer

0 Comments