Silverlight - passing in Windows username to Principal.Login

Silverlight - passing in Windows username to Principal.Login

Old forum URL: forums.lhotka.net/forums/t/6556.aspx


pondosinat posted on Wednesday, March 04, 2009

I need to make a call to the webserver from Silverlight to retrieve the user's windows domain name, presumably from my Security.Identity class. So it's just a simple string function. What's the easiest way to do this? Use the Dataportal and go through the BeginFetch etc..? or is there a simpler synchronous way to achieve this?

RockfordLhotka replied on Wednesday, March 04, 2009

If I understand you correctly, this scenario is already supported by CSLA .NET for Silverlight. Look under the Tests download and you'll find a number of applications to test various authentication models, including one to pull the server's Windows identity back to the client.

There's a little more too it than you might expect, because you need to ensure that the web server runs service calls under the right identity, otherwise it is a relatively straightforward process.

pondosinat replied on Wednesday, March 04, 2009

Thank you. Just to be clear - what I'm going after is being able to use the CSLA's model of SQL based roles, but just using the windows username as the tie in to the CSLA "Users" table. So the initial call I'm asking about is really just to pass in the windows username into the Principal.Login method (with no password), since the fact that the user is authenticated with a valid username would be sufficient.

With that in mind, would it make sense to do a simple GetUserName call to the web server and if so how would I achieve that?

 

Thanks

RockfordLhotka replied on Wednesday, March 04, 2009

The example I mentioned is still a good resource. To get the username from the server, you need to ensure that the service you are calling actually runs under the user’s identity (and of course the user must have already logged into the web site with their Windows credentials).

 

You certainly can do this with a simple asmx or WCF web service – as long as you ensure the service runs under the right identity.

 

But you could also do it as part of your login process, without a separate server call.

 

Why not just send your login request through the data portal to the web server, grab the user’s Windows identity to get the name, and then use that name to get the data from the database? All done in one trip to the server.

 

Rocky

 

pondosinat replied on Wednesday, March 04, 2009

Yes, your one trip approach is much better! I found yet another option that I thought I would detail for anyone else who may be interested. This approach makes use of the InitParameters of your ASP.NET Silverlight control. In the asp.net startup page, simply set an input parameter as such:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Xaml1.InitParameters = "username=" & System.Security.Principal.WindowsIdentity.GetCurrent().Name

End Sub

Then use this value in your silver light app like so:

Private Sub Application_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)

Dim usernameValue As String = e.InitParams("username")

End Sub

Finally, simply pass in usernameValue into the CSLA login method, and you're logged in...

RockfordLhotka replied on Wednesday, March 04, 2009

That’s a great tip, thanks!!

 

Rocky

 

Copyright (c) Marimer LLC