CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2012
    Posts
    5

    check user is admin or not?

    Hi guys,
    I have budget form and i want to display that only when user is admin.
    I have "users" table and userID is primary key. there is field "role".
    And role column contain different name like approver,user,admin and etc...
    now I want that if user role is admin then budget page should display only.

    I have following code for login.
    ----------------------------------------------------------------------------------------------------------------------
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {



    }

    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
    try

    {

    string uname = Login1.UserName.Trim(); //Get the username from the control

    string password = Login1.Password.Trim(); //get the Password from the control
    Session.Add("username",uname);

    bool flag = AuthenticateUser(uname, password);

    if (flag == true)

    {

    e.Authenticated = true;

    Login1.DestinationPageUrl = "disclamer.aspx";

    }

    else

    e.Authenticated = false;

    }

    catch (Exception)

    {

    e.Authenticated = false;

    }
    }
    private bool AuthenticateUser(string uname, string password)

    {

    bool bflag = false;

    string connString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\fpd.mdf ;Integrated Security=True;User Instance=True";

    string strSQL = "select * from users where userID ='" + uname + "' AND password ='" + password + "'";

    DataSet userDS = new DataSet();

    SqlConnection m_conn;

    SqlDataAdapter m_dataAdapter;

    SqlCommand m_Command;

    try

    {

    m_conn = new SqlConnection(connString);

    m_conn.Open();

    m_dataAdapter = new SqlDataAdapter(strSQL, m_conn);

    m_dataAdapter.Fill(userDS);

    m_conn.Close();

    }

    catch (Exception ex)

    {

    userDS = null;

    }



    if (userDS != null)

    {

    if(userDS.Tables[0].Rows.Count > 0)

    bflag = true;

    }

    return bflag;

    }
    }
    ------------------------------------------------------------------------------------------------------------------------

    So can anybody give me idea please?

    Thanks a lot.

  2. #2
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Re: check user is admin or not?

    Have a look at WindowsUser and WindowsPrincipal classes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured