Advertisement
Guest User

NOTAdmin

a guest
Sep 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using OnlineStore.DAL;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7.  
  8. namespace OnlineStore.Models
  9. {
  10.     public class NotAdminAuthorization : AuthorizeAttribute // Authorize ALL except Admin
  11.     {
  12.         User myUser = new User();
  13.         protected override bool AuthorizeCore(HttpContextBase httpContext)
  14.         {
  15.             //var isAuthorized = base.AuthorizeCore(httpContext);
  16.             //if (!isAuthorized)
  17.             //    return false;
  18.  
  19.             string CurrentUser = httpContext.User.Identity.Name; // Current UserName //
  20.             DataLayer dal = new DataLayer();
  21.  
  22.             List<User> usr =
  23.                 (from x in dal.Users
  24.                  where x.UserName == CurrentUser
  25.                  select x).ToList<User>();
  26.  
  27.             if (usr.Count == 1)
  28.             {
  29.                 myUser.UserName = usr[0].UserName;
  30.                 myUser.Password = usr[0].Password;
  31.                 myUser.Permission = usr[0].Permission;
  32.                 if (myUser.Permission == "Admin")
  33.                     return false;
  34.             }
  35.  
  36.             return true;
  37.         }
  38.         protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
  39.         {
  40.             filterContext.Result = new HttpUnauthorizedResult();
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement