locked
Deleted RRS feed

All replies

  • I've posted a solution for forms authentication here. As it was the wrong thread I do bring it over here.

    As the LS Silverlight Desktop Client uses ClientHttp stack, there is no automatic credentials/cookie management (see here, Automatic cookie handling -> Browser only).

    This is the reason why ServerApplicationContext.CreateContext will trigger a server 500 error indicating that a web api request is not authenticated.

    Unfortunately, there is no direct way to get from the Silverlight client respective credentials needed for  WebClient or HttpWebRequest.

    When using forms authentication, you can however get the ASP.net session token using the following:

    1. Use the AuthenticationService of your screen  to get AuthenticationOperation of the actual user.
    2. Get the internal LoadUserOperation and Servicefield/property.
    3. Finally, get the CookieContainer from the service

    This cookie can then be used in your web request.

    Dim userState As Object = Nothing Dim cc As System.Net.CookieContainer = Nothing Dim auths = <YourScreen>.Details.Application.AuthenticationService.LoadUser(AddressOf loadDone, userState) 'Microsoft.LightSwitch.Security.ClientGenerated.Implementation.Internal.AuthenticationOperation Dim op = GetPrivateField(Of Object)(auths, "operation") 'System.ServiceModel.DomainServices.Client.ApplicationServices.LoadUserOperation Dim svc = GetPrivateProperty(Of Object)(op, "Service") 'Microsoft.LightSwitch.Security.ClientGenerated.Implementation.Internal.CustomAuthenticationService cc = GetPrivateProperty(Of Object)(svc, "CookieContainer") 'direct cc = TryCast(svc.GetType.GetProperty("CookieContainer").GetValue(svc, Nothing), System.Net.CookieContainer) Private Sub loadDone(auth As Microsoft.LightSwitch.Security.Client.IAuthenticationOperation) End Sub Private Function GetPrivateProperty(Of T)(obj As Object, name As String) As T Dim flags As BindingFlags = BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public Dim type As Type = obj.[GetType]() Dim field As PropertyInfo = type.GetProperty(name, flags) Return DirectCast(field.GetValue(obj, Nothing), T) End Function Private Function GetPrivateField(Of T)(obj As Object, name As String) As T Dim flags As BindingFlags = BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Static Dim type As Type = obj.[GetType]() Dim field As FieldInfo = type.GetField(name, flags) Return DirectCast(field.GetValue(obj), T) End Function

    'usage with HttpClient
    
    Dim handler As HttpClientHandler = New HttpClientHandler()
    handler.CookieContainer = cc
    handler.UseDefaultCredentials = False
    
    
    'usage with WebRequest
    Dim request As HttpWebRequest = DirectCast(WebRequest.Create(cmdUri), HttpWebRequest)               
    request.CookieContainer = cc





    • Edited by mf_falkao Wednesday, August 14, 2013 7:48 AM
    • Proposed as answer by andysheu Saturday, April 26, 2014 11:09 AM
    Tuesday, August 13, 2013 4:19 PM