Reputation: 165
I'm trying to build a application that validates your login with Windows via the Logon User Dll Import function. I have it working, but now I would like to ADD the ability for the user to be authenticated over my SQL database and given a Role. For example, user logins and receives role from SQL and gets authorized by logon user. That role is Administrator,Management,User in the order of importance. Not really sure what is wrong. Any help would be appreciated.
Here is my C# Code
FormsAuthentication.Initialize();
string roles = string.Empty;
var conn = @"Data Source = localhost;Initial Catalog=web";
using (var con = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand("Get_User_Role"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Username", btnLogin);
cmd.Connection = con;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
roles = reader["Roles"].ToString();
con.Close();
}
}
Here is my web.config
:
<authentication mode="Forms">
<forms loginUrl="login.aspx" name=".ASPXAUTH" protection="All" path="/" defaultUrl="Default.aspx" timeout="30" ></forms>
</authentication>
<authorization>
<deny users="?"/>
<allow roles="Administrators, Managers,Users"/>
<allow users="*"/> <!-- might have to delete this to work, which is ok. Better to have and not need then to need and not have. -->
</authorization>
Here is my button login from ASP
<asp:Button ID="btnLogin" runat="server" width="315px" Cssclass="btn"
onclick="btnLogin_Click" Text="Login" ></asp:Button>
I run it and it stops at con.Open
saying incorrect username.
I also was told I needed to add a list for either Administrator, Manager, User. I shouldn't haft too with roles=reader["Roles"].ToString();
right?
Upvotes: 0
Views: 82
Reputation: 261
I run it and it stops at con.Open saying incorrect username.
You have a problem with your connection string, check the links below:
FormsAuthentication.Initialize();
string roles = string.Empty;
var conn = @"Data Source = localhost;Initial Catalog=web"; // Here your problem
using (var con = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand("Get_User_Role"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Username", btnLogin);
cmd.Connection = con;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
roles = reader["Roles"].ToString();
con.Close();
}
}
Upvotes: 1