|
-
Jul 18th, 2012, 04:15 PM
#1
Thread Starter
Member
[RESOLVED] How do i add user role's to asp.net using MS Access DB
Hi.
(First sry my english)
Im new to this and is trying to get a login script working, its working fine, but now i have a new problem, i have a field im my DB thats called "urole" in that i have 3 roles, Admin, Co-Admin, User.
How can i use something like this on a page.
Code:
If Roles.IsUserInRole("Admin") Then
Label1.Text = User.Identity.Name + " is an Admin"
Else
Label1.Text = User.Identity.Name + " is NOT in an Admin"
End If
I have this script on my login page
Code:
Imports System.IO
Imports System.Data
Imports System.Data.OleDb
Imports System.Web.Configuration
Partial Class DbLogin
Inherits System.Web.UI.Page
Public Function Myauthenticate(ByVal username As String, ByVal userPassword As String) As Boolean
Using conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString)
Try
conn.Open()
Catch ex As Exception
' Log the error but don't
' display any details to the user
System.Diagnostics.Debug.WriteLine("Exception: " & ex.Message)
' Login failed
Return False
End Try
Try
Using cmd As New OleDbCommand()
cmd.Connection = conn
cmd.CommandText = "" & _
"SELECT urole " & _
"FROM usersdatabase " & _
"WHERE uname=@userName " & _
"AND pwd=@passWord"
cmd.Parameters.AddWithValue("@userName", username)
cmd.Parameters.AddWithValue("@passWord", userPassword)
Dim Retuser As String = cmd.ExecuteScalar().ToString()
If Retuser IsNot Nothing Then
Return True
Else
Return False
End If
End Using
Catch ex As Exception
' Log the error but don't
' display any details to the user
System.Diagnostics.Debug.WriteLine("Exception: " & ex.Message)
' Login failed
Return False
Finally
conn.Close()
End Try
End Using
Dim MyConfig As Configuration = WebConfigurationManager.OpenWebConfiguration("./")
Dim SystemWeb As ConfigurationSectionGroup = MyConfig.SectionGroups("system.web")
Dim AuthSec As AuthenticationSection = DirectCast(SystemWeb.Sections("authentication"), AuthenticationSection)
AuthSec.Forms.Credentials.Users.Add(New FormsAuthenticationUser(UsernameText.Text, PasswordText.Text))
MyConfig.Save()
End Function
Protected Sub LoginAction_Click(
ByVal sender As Object, ByVal e As System.EventArgs
) Handles LoginAction.Click
Page.Validate()
If Not Page.IsValid Then Return
If Me.Myauthenticate(UsernameText.Text, PasswordText.Text) Then
FormsAuthentication.RedirectFromLoginPage(UsernameText.Text, False)
Else
LegendStatus.Text = "Invalid username or password!"
End If
End Sub
End Class
and then i have this on my default page, thats shows the info from an user, now i just want to show the role (urole) and then use an "If Roles.IsUserInRole" event so i can show some diffrent thing if admin, co-admin or User
Code:
Imports System.IO
Imports System.Web.Security
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim htmlString As New StringBuilder()
' Has the request been authenticated?
If Request.IsAuthenticated Then
' Display generic identity information.
' This is always available, regardless of the type of
' authentication.
htmlString.Append("<h3>Generic User Information</h3>")
htmlString.Append("<b>name: </b>")
htmlString.Append(User.Identity.Name)
htmlString.Append("<br><b>Authenticated With: </b>")
htmlString.Append(User.Identity.AuthenticationType)
htmlString.Append("<br><br>")
End If
' Was forms authentication used?
If TypeOf User.Identity Is FormsIdentity Then
' Get the ticket.
Dim ticket As FormsAuthenticationTicket = (DirectCast(User.Identity, FormsIdentity)).Ticket
htmlString.Append("<h3>Ticket User Information</h3>")
htmlString.Append("<b>Name: </b>")
htmlString.Append(ticket.Name)
htmlString.Append("<br><b>Issued at: </b>")
htmlString.Append(ticket.IssueDate)
htmlString.Append("<br><b>Expires at: </b>")
htmlString.Append(ticket.Expiration)
htmlString.Append("<br><b>Cookie version: </b>")
htmlString.Append(ticket.Version)
' Display the information.
LegendInfo.Text = htmlString.ToString()
End If
End Sub
Protected Sub SignOutAction_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SignOutAction.Click
FormsAuthentication.SignOut()
FormsAuthentication.RedirectToLoginPage()
End Sub
End Class
Hope someone can help me, im new to this and cant get this fixed.
(The aboved is working fine, but its not showing the urole/Role of an user)
-
Jul 18th, 2012, 06:56 PM
#2
Re: How do i add user role's to asp.net using MS Access DB
Hi.Sorry i don't get it.
Have you set up asp.net membership database using Access?If so delete and start over with sql express.Enough with this Access crap.
So if you have set up asp.net membership you should use the fields provided by the database that are used for the roles, specifically dbo.aspnet_Roles table.
If you want to use a custom column for roles you will have to either modify the database sproc's or overload the membership object or both.Do you really want to go to that trouble bearing in mind that the functionality has already being created by visual studio?
Now if you use a custom database then Roles.IsUserInRole will not work to a custom database or anyhow you can somehow simulate it but the membership object will have to be re created.
To cut a long story sort just use the provided functionality.
http://msdn.microsoft.com/en-us/library/ff648345.aspx
P.S. Ok just for the heck of it i played with the database a little and found that you can put another column to get your users roles with some modifications.But this will involve modifying the asp.net database that already has the functionality so it's completely pointless.
Last edited by sapator; Jul 18th, 2012 at 09:03 PM.
Reason: snorting microchips
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 19th, 2012, 01:05 AM
#3
Re: How do i add user role's to asp.net using MS Access DB
 Originally Posted by sapator
Have you set up asp.net membership database using Access?If so delete and start over with sql express.Enough with this Access crap.
I have to agree with this point. (Although Sap, watch your language )
If you are not tied to using an existing Access Database (i.e. you already have data that you are trying to re-use) I would highly recommend that you switch to using SQL Server Express. This will allow you to scale much easier as your application grows in size.
Can you confirm whether you have an existing database? If this is the case, then the only real option, assuming that you want to use the Role Provider, is to implement your own custom Role Provider.
Here is an example of how you would go about doing that:
http://msdn.microsoft.com/en-us/library/tksy7hd7
Gary
-
Jul 19th, 2012, 03:58 AM
#4
Thread Starter
Member
Re: How do i add user role's to asp.net using MS Access DB
Hi u 2.
Thx for reply.
Im using a database with only 4 fields, bc the database that VS2010 is provided is with 3-4 tabels and with alot of fields that im not is going to use.
i know that its better with SQL db then MS db.
-
Jul 19th, 2012, 04:53 AM
#5
Re: How do i add user role's to asp.net using MS Access DB
Hello,
In which case, you use the built in "Roles" classes within your code, you are going to need to implement your own custom Role Provider, as per the link above.
Gary
-
Jul 19th, 2012, 08:49 AM
#6
Thread Starter
Member
Re: How do i add user role's to asp.net using MS Access DB
Hi again
If i use this EX.
http://support.microsoft.com/kb/308157
is it easier to add the roll event to that ex. !? or do i stille have the same problem with the custom Database, this time just in SQL !?
-
Jul 19th, 2012, 11:01 AM
#7
Re: How do i add user role's to asp.net using MS Access DB
Hey,
Sorry, I am confused by your question.
Gary
-
Jul 19th, 2012, 11:21 AM
#8
Thread Starter
Member
Re: How do i add user role's to asp.net using MS Access DB
if i can't use MS Access and SQL is better, is the login script i linked to then OK and how do i add Roles to that, or is it the same problem as with the MS Access DB that the database is custom provided !?
-
Jul 19th, 2012, 12:19 PM
#9
Thread Starter
Member
Re: How do i add user role's to asp.net using MS Access DB
Okay i got it
After looking on these links and smash it together then i got and script/code that can handel the roles.
http://support.microsoft.com/kb/308157
http://www.4guysfromrolla.com/articles/082703-1.2.aspx
http://forums.asp.net/t/1419687.aspx/1
Thx anyway
-
Jul 20th, 2012, 01:52 AM
#10
Re: How do i add user role's to asp.net using MS Access DB
Hello,
Would it be possible for you to share exactly what you did? This is a common question that gets asked on the forums, so I am sure that other people will find it useful.
Also, can you please remember to mark your thread as resolved?
Gary
-
Jul 20th, 2012, 02:28 AM
#11
Thread Starter
Member
Re: How do i add user role's to asp.net using MS Access DB
Hi
I will do that later today, and i will mark my thread as resolved, when i get back later today.
-
Jul 21st, 2012, 10:55 AM
#12
Thread Starter
Member
Re: How do i add user role's to asp.net using MS Access DB
Hi i have made a Code Snip to this Thread see the code snip here.
ASP.NET Login Script authenticate and role based, with MS Access DB and SQL - PART 1. http://www.vbforums.com/showthread.php?t=684701
ASP.NET Login Script authenticate and role based, with MS Access DB and SQL - PART 2. http://www.vbforums.com/showthread.php?t=684702
ASP.NET Login Script authenticate and role based, with MS Access DB and SQL - PART 3. http://www.vbforums.com/showthread.php?p=4202608
Again Thx
-
Jul 23rd, 2012, 12:54 AM
#13
Re: [RESOLVED] How do i add user role's to asp.net using MS Access DB
Hello,
Thanks for posting those links, I am sure that these will be useful to someone.
Out of interest, is there a reason why you did not implement your own custom membership/roles provider?
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|