473,394 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

ASP.NET Login and authentication

JamieHowarth0
533 Expert 512MB
Hi folks,

Got a bit of an interesting question.
I'm in the process of learning ASP.NET using Microsoft's CTP of Visual Web Studio "Orcas". Part of my classic ASP website incorporates a login section where if someone logging in belongs to my corporate Active Directory (i.e. employees), then I use their given credentials to connect to my SQL Server database (and Exchange Web Services) to create a very nice, pretty, fully-integrated CRM solution so they can get on with their work.
If it's one of my clients (with an email address) then it picks this up and does exactly the same thing - BUT retrieves their login details from a table stored within this database, accessed using the IIS_MACHINE anonymous account.
Having looked over the asp:Login and asp:SqlDataSource, I'm a little lost (being a complete newbie to the whole .NET thing - although it looks pretty powerful from what I've seen so far). How would I go about doing this?
Example code:
Expand|Select|Wrap|Line Numbers
  1. <script runat="server">
  2.     Function CheckAuthCreds(ByVal myUserName As String, ByVal myPassword As String) As Boolean
  3.         'Two-step login - check type of login requested (staff or client)
  4.         'If staff - Windows authentication and relevant features built into Exchange/SQL Server etc.
  5.         'If client - SQL server MD5-encrypted password check and limited Exchange
  6.         Select Case InStr(myUserName, "@")
  7.             Case Is > -1
  8.                 'We know this is a client email login. Run SQL query to find details.
  9.                 myDbConn.SelectCommand = "SELECT * FROM tbl_Clients WHERE EmailAddr = '" & myUserName & "' AND UserPwd = '"
  10.  
  11.                 Dim hashedDataBytes As Byte()
  12.                 Dim encoder As New UTF8Encoding()
  13.                 Dim md5Hasher As New System.Security.Cryptography.MD5CryptoServiceProvider
  14.                 hashedDataBytes = md5Hasher.ComputeHash(encoder.GetBytes(myPassword))
  15.                 Dim b As Byte
  16.                 For Each b In hashedDataBytes
  17.                     myDbConn.SelectCommand = myDbConn.SelectCommand & b
  18.                 Next b
  19.                 myDbConn.SelectCommand = myDbConn.SelectCommand & "'"
  20.                 MsgBox(myDbConn.SelectCommand) 'Just to test that I got the MD5 stuff correct
  21.                 Return True
  22.             Case Else
  23.                 'This is a staff login, use WinAuth to verify ID against SQL Server.
  24.                 'THIS IS WHERE I'M STUCK!!! I can't seem to connect to the SQL Server DB using the staff username and password
  25.                 '(cause the connection string is stored in web.config file and I don't know how to alter it)
  26.         End Select
  27.     End Function
  28.  
  29.     Sub DoLogin(ByVal sender As Object, ByVal e As AuthenticateEventArgs)
  30.         Dim blIsAuthed As Boolean
  31.         blIsAuthed = CheckAuthCreds(loginbox.UserName, loginbox.Password)
  32.  
  33.         e.Authenticated = blIsAuthed
  34.         If e.Authenticated = True Then
  35.         Else
  36.             MsgBox("Login failed")
  37.         End If
  38.     End Sub
  39. </script>
  40.     <title>My website</title>
  41. </head>
  42. <body>
  43.     <table style="width: 100%;">
  44.         <tr>
  45.             <td>
  46.                 <form runat="server">
  47.                     <asp:SqlDataSource runat="server" ConnectionString="<%$ ConnectionStrings:mydb %>" ID="myDbConn">
  48.                     </asp:SqlDataSource>
  49.                     <asp:Login ID="loginbox" runat="server" OnAuthenticate="DoLogin">
  50.                     </asp:Login>
  51.                 </form>
  52.             </td>
  53.         </tr>
  54.     </table>
  55. </body>
  56. </html>
  57.  
Hope that someone can help!
Many thanks in advance,

medicineworker/MPDDK-mm4
Jul 18 '07 #1
3 1879
TRScheel
638 Expert 512MB
Try this with your connection string in web.Config:

myConnectionString = "...... username={0} ... password={1}....."

then in the code use this:

Expand|Select|Wrap|Line Numbers
  1. Dim username as string = "John Doe"
  2. Dim password as string = "drowssap"
  3. Dim TheConnectionStringYouWillUse as string = string.Format(MyConnectionString, username, password)
  4.  
That will replace the {0} with the user name and the {1} with the password.
Jul 18 '07 #2
JamieHowarth0
533 Expert 512MB
Hi TRScheel,

Brilliant, worked first time!
Now clients can login using IIS_Anonymous account to access/edit their data and my colleagues can login using their own Active Directory usernames/passwords to administer client info!

Many thanks,

medicineworker
Jul 18 '07 #3
TRScheel
638 Expert 512MB
Anytime, glad to help
Jul 19 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: buran | last post by:
Dear ASP.NET Programmers, How can I post data to an ASP.NET login page and pass authentication? The login page uses forms authentication, users must supply usernames and password and have to...
5
by: Matthew Louden | last post by:
I wrote ASP.NET application that access SQL Server database. When I run the application, it yields "Login failed for user '<COMPUTER_NAME>\ASPNET'" error message. I then did the following, but...
11
by: David W. Simmonds | last post by:
I have a form that will prompt for a user name/password. In VS.NET, I have the protected form in a folder named Admin. I have a Web.config file in that folder as well. It contains the following...
4
by: nicholas | last post by:
Hi, Got an asp.net application and I use the "forms" authentication mode defined in the web.config file. Everything works fine. But now I would like to add a second, different login page for...
2
by: pv | last post by:
Hi everyone, I need help with following scenario, please: Users are accessing same web server from intranet (users previously authenticated in Active Dir) and from extranet (common public...
10
by: et | last post by:
I have an asp.net program that uses a connection string, using integrated security to connect to a sql database. It runs fine on one server, but the other server gives me the error that "Login...
9
by: dana lees | last post by:
Hello, I am developing a C# asp.net application. I am using the authentication and authorization mechanism, which its timeout is set to 60 minutes. My application consists of 2 frames - a...
7
by: Alan Silver | last post by:
Hello, Sorry this is a bit wordy, but it's a pretty simple question... I have a web site, http://domain/ which is a public site, part of which (http://domain/a/) is protected by forms...
7
by: Samuel Shulman | last post by:
Is there a method that will indicate the person who logged successfully is Logged and therefore allowed to browse freely other then using the...
6
by: Kat | last post by:
Every time I attempt to run a localhost website, it asks me for a login, as if I am not a user on the local machine. I am a user on the local machine, I am an admin on the local machine. I am not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.