Results 1 to 13 of 13

Thread: [RESOLVED] How do i add user role's to asp.net using MS Access DB

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    48

    Resolved [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)

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: How do i add user role's to asp.net using MS Access DB

    Quote Originally Posted by sapator View Post
    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

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    48

    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.

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    48

    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 !?

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: How do i add user role's to asp.net using MS Access DB

    Hey,

    Sorry, I am confused by your question.

    Gary

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    48

    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 !?

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    48

    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

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  11. #11

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    48

    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.

  12. #12

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    48

    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

  13. #13
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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
  •  



Click Here to Expand Forum to Full Width