Link to home
Start Free TrialLog in
Avatar of milani_lucie
milani_lucieFlag for United States of America

asked on

Authorization using custom roles from DB - WCF

SecurityLib.dll
 
Public Class MyCustomValidator
    Inherits UserNamePasswordValidator

    Public Shared _roles As New List(Of String)

    Public Overrides Sub Validate(ByVal userName As String, ByVal password As String)
        If (userName <> "Mike" OrElse password <> "Sam") Then
            Throw New FaultException("Invalid Credentials !")
        Else
            Dim str As String = "User,Admin"
            _roles.AddRange(str.Split(",").ToList())
        End If
    End Sub

End Class
 
ServiceLib.dll

Public Class AddClass
    Implements IAdd

    Public Function AddFunc(ByVal value1 As Integer, ByVal value2 As Integer) As Integer Implements IAdd.AddFunc
        If SecurityLib.MyCustomValidator._roles.Contains("Admin") Then
            Return (value1 + value2)
        End If
    End Function

End Class

Client

Sub Main()
       Dim client As New AddProxy.AddClient
       client.ClientCredentials.UserName.UserName = "Mike"
       client.ClientCredentials.UserName.Password = "Sam"
       Console.WriteLine("Addition of two numbers = " & client.AddFunc(10, 20))
       client.Close()      
       Console.ReadLine()
End Sub

My problem here is: I have delcared the variable (_roles) as SHARED in SECURITYLIB.dll. So it will be shared among ALL instances as it belongs to CLASS not to an instance. Now if two or more users are working on the same application, then i will lose the previous user roles values. If it is NOT a SHARED variable (_roles), then i need to create an instance of SECURITYLIB.dll which will make the variable (_roles) as "" (Empty String) or may be NULL. Also please note that in WCF security using UserName and Password
 
Public Overrides Sub Validate(ByVal userName As String, ByVal password As String)
 
will be called first before

Public Function AddFunc(ByVal value1 As Integer, ByVal value2 As Integer) As Integer Implements IAdd.AddFunc

Can you please let me know, how can i tackle this situation ?

Thanks
Avatar of Darren
Darren
Flag of Ireland image

Have you looked into using SQL Server Role Provider instead.

http://msdn.microsoft.com/en-us/library/ff647040.aspx

Cheers,

Darren
Avatar of milani_lucie

ASKER

I am working on Console Application - Both Host and Client. Does the above URL helps  :(  ? BTW: I hate MSDN. You can provide me any article which can be easily understandable on this for implementation !!

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Darren
Darren
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nope...That will not work. Can you please provide me another way of doing this ?

Thanks
Provided solution will not work.