The C# documentation on remoting shows that there are C# style extension methods that are available with having using WebSharper.Web;.

This is how you can log in then get user name:

1
2
3
4
5
6
7
8
9
10
11
12
13
// using WebSharper.Web;
[Remote]
public static async Task<string> DoSomething(string input)
{
    var ctx = WebSharper.Web.Remoting.GetContext();
    await ctx.UserSession.LoginUserAsync(input); // extension member
    var username = await ctx.UserSession.GetLoggedInUserAsync(); // extension member
    // after being logged in, this seems to work properly too, giving same result:
    var httpContext = ctx.Environment["HttpContext"] as HttpContextBase;
    var usernameAgain = httpContext.User.Identity.Name;

    return username;
}

Hope this helps, we will be updating the docs for clarity. (You can also post questions/issues and pull request for documentation here)

By on 10/29/2018 6:27 AM ()

I suspect that the above is working only for a dot.net Core project but not on the classic Framework.

Notice also that LoginUserAsync seems a Form login, not related to Windows Identity.

By on 10/29/2018 3:53 PM ()

@JankoA Thank you very much! It works like a charm!

(fyi I set the debug configuration for windows authentication according to this and I used just the first username you have shown above)

btw for the second option of usernameAgain in my Environment the key was "WebSharper.AspNetCore.HttpContext"

1
var httpContext = ctx.Environment["WebSharper.AspNetCore.HttpContext"] as HttpContext;

but the first option of username was already perfect.

By on 10/29/2018 7:06 AM ()

I've also tried

1
2
3
4
5
6
var usernameAsyncAsync = ctx.UserSession.GetLoggedInUser();
var usernameOption = FSharpAsync.RunSynchronously
    (usernameAsyncAsync, 
    timeout: FSharpOption<int>.None, 
    cancellationToken: FSharpOption<CancellationToken>.None);
var userName = usernameOption?.Value;

without success either

By the way GetLoggedInUser should not expose F# Async to C# code because, according to Tomas Petricek

It is generally not recommended to expose F# types such as FSharpAsync in a public interface that will be used by C# clients

By on 10/28/2018 12:54 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper