locked
How to check Role using Linq RRS feed

  • Question

  • I tried to check Role When login a form. When client log in it will check if is user it will response.redirect("Details.aspx")

    and if is Admin it will response.redirect("AddNew.aspx"). if not a user or admin, it will response.redirect("index.aspx")

    But it not work. I don't know how. Please help me. Thank you so much. 

    p/s: I'm Student and i am learning C#. If there is anything wrong, plz just let me know. Thank you.

    create table TBL_Users
    (
    UserID varchar(20) primary key,
    Password varchar(20) not null,
    isAdmin bit
    )

    public class Class1
    {
    	public Class1()
    	{
    		
    	}
        private bool admin;
        public bool IsAdmin
        {
            get { return admin;}
            set { admin = value; }
        }
        public bool LoadData(string name, string password)
        {
                DataDataContext data = new DataDataContext();
                    var x = from y in data.TBL_Users
                        where y.UserID == name && y.Password == password
                        select y;
                    if (x.Count() != 0)
                    {
                        foreach (var item in x)
                        {
                            if (item.isAdmin.Value)
                            {
                                IsAdmin = item.isAdmin.Value;
                            }
                            else
                            {
                                IsAdmin = item.isAdmin.Value;
                            }
                        }
                    }
                    return IsAdmin;
                 
    }  
        }
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        Class1 classdata = new Class1();
        protected void Button1_Click(object sender, EventArgs e)
        {
            bool flag = false;
            if (classdata.LoadData(txtUserName.Text, txtPassword.Text))
            {
                if (classdata.IsAdmin)
                {
                    Response.Redirect("AddNew.aspx");
                   
                }
                else
                {
                    Response.Redirect("Details.aspx");
                }
                flag = true;
            }
            if(!flag)
                {
                    Response.Redirect("Login.aspx");
                }
    }



    • Edited by loganxnt Wednesday, August 28, 2013 5:27 PM
    Wednesday, August 28, 2013 12:40 PM

Answers

  • Hello,

    Welcome toMSDN Support Forum.

    I reviewed your code and found the problem may be that you want to judge three different roles: User, Admin, DefaultLoginUser, however the method LoadData() you call will return true or false only.

    So let us assume that it returns a true, it means that it logins success for admin.

    And if LoadData() returns the false, in your code it actually cannot judge whether it logins false or

    logins success for user.

    So you should change your LoadData() to be below:

    public bool LoadData(string name, string password)
            {
                DataClasses1DataContext data = new DataClasses1DataContext();
                var x = from y in data.TBL_Users
                        where y.UserID == name && y.Password == password
                        select y;
                if (x.Count() != 0)
                {
                    foreach (var item in x)
                    {
                        if (item.isAdmin.Value)
                        {
                            IsAdmin = item.isAdmin.Value;
                        }
                        else
                        {
                            IsAdmin = item.isAdmin.Value;
                        }
                    }
                    return true;
                }
                return false;
            }
    

    I look forward to hearing from you.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, August 29, 2013 4:00 AM

All replies

  • Hello,

    Welcome toMSDN Support Forum.

    I reviewed your code and found the problem may be that you want to judge three different roles: User, Admin, DefaultLoginUser, however the method LoadData() you call will return true or false only.

    So let us assume that it returns a true, it means that it logins success for admin.

    And if LoadData() returns the false, in your code it actually cannot judge whether it logins false or

    logins success for user.

    So you should change your LoadData() to be below:

    public bool LoadData(string name, string password)
            {
                DataClasses1DataContext data = new DataClasses1DataContext();
                var x = from y in data.TBL_Users
                        where y.UserID == name && y.Password == password
                        select y;
                if (x.Count() != 0)
                {
                    foreach (var item in x)
                    {
                        if (item.isAdmin.Value)
                        {
                            IsAdmin = item.isAdmin.Value;
                        }
                        else
                        {
                            IsAdmin = item.isAdmin.Value;
                        }
                    }
                    return true;
                }
                return false;
            }
    

    I look forward to hearing from you.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, August 29, 2013 4:00 AM
  • Thank for Your Help Mr. Fred Bao.

    I just changed my LoadData() and it work. I really confused with LoadData() and i don't know how to fix it. And Now it work...

    In this Login.aspx. I checked if User logins success or false. 

    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        Class1 classdata = new Class1();
        protected void Button1_Click(object sender, EventArgs e)
        {
            bool flag = false;
            if (classdata.LoadData(txtUserName.Text, txtPassword.Text))
            {
                if (classdata.IsAdmin)
                {
                    Response.Redirect("AddNew.aspx");
                   
                }
                else
                {
                    Response.Redirect("Details.aspx");
                }
                flag = true;
            }
            if(!flag)
                {
                    Response.Redirect("Login.aspx");
                }
    }

    I am really happy. I really really thank you so much. You are really help me. 

    I'm a student, and there are so many thing i have to learn. And maybe i will ask you more . Is it Possible?

    Thank you so much. 


    • Edited by loganxnt Thursday, August 29, 2013 12:19 PM
    Thursday, August 29, 2013 11:42 AM