mvc page Authorization using role based

Last Reply 9 days ago By dharmendr

Posted 12 days ago

Kinldy proide brief explanination ,Below class file i created for fatch user role

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

namespace OEMMVC
{
    public class MyRoleProvider : RoleProvider
    {
        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetRolesForUser(string username)
        {
            // throw new NotImplementedException();

            SqlConnection conOEM = new SqlConnection(ConfigurationManager.ConnectionStrings["conOEM"].ToString());
            conOEM.Open();
        
            // throw new NotImplementedException();
            SqlCommand cmd = new SqlCommand("select * from OEMUserAccess", conOEM);

            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            da.Fill(ds);
            string result1 = "";

            string[] array = new string[0];
            if (ds.Tables[0].Rows.Count != 0)
            {
                int a = ds.Tables[0].Rows.Count;
                array = new string[a];
                for (int i = 0; i < a; i++)
                {


                    array[i] = ds.Tables[0].Rows[i]["Role"].ToString();

                }
                result1 = ConvertStringArrayToString(array);


            }
            return array;
        }
        static string ConvertStringArrayToString(string[] array)
        {
            //
            // Concatenate all the elements into a StringBuilder.
            //
            StringBuilder builder = new StringBuilder();
            foreach (string value in array)
            {
                builder.Append(value);
                builder.Append(',');
            }
            return builder.ToString();
        }
        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}

This my Controller Funcation 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OEMMVC.Models;
using OEMMVC.AuthData;
using System.Web.Mvc;
using System.IO;
using System.Configuration;

namespace OEMMVC.Controllers
{
    [AuthAttribut]
     [Authorize]
    // Developed by :Venkatachalam G Start Date:24-08-2018
    public class OEMViewController : Controller
    {
        // GET: OEMView
        [Authorize(Roles = "BUYER,HPM")]
       [HttpGet]
        public ActionResult ViewOEM()
        {
            OEMEntry objoem = new OEMEntry();
            DataAccessLayer objds = new DataAccessLayer();
            objoem.Showalloem = objds.Selectalldata();
            return View(objoem);
        }
}
}

Web Config

 

 

  <roleManager enabled="true" defaultProvider="MyProvider"> 
    <providers>
      <clear/>
      <add name="MyProvider" type="OEMMVC.MyRoleProvider"/>
    </providers>
  </roleManager>

But i did't get Event fired below method from My Role provider class

 public override string[] GetRolesForUser(string username)
        {
            // throw new NotImplementedException();

            SqlConnection conOEM = new SqlConnection(ConfigurationManager.ConnectionStrings["conOEM"].ToString());
            conOEM.Open();
        
            // throw new NotImplementedException();
            SqlCommand cmd = new SqlCommand("select * from OEMUserAccess", conOEM);

            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            da.Fill(ds);
            string result1 = "";

            string[] array = new string[0];
            if (ds.Tables[0].Rows.Count != 0)
            {
                int a = ds.Tables[0].Rows.Count;
                array = new string[a];
                for (int i = 0; i < a; i++)
                {


                    array[i] = ds.Tables[0].Rows[i]["Role"].ToString();

                }
                result1 = ConvertStringArrayToString(array);


            }
            return array;
        }
You are viewing reply posted by: dharmendr 9 days ago.