Solved

Access User Name in application

Posted on 2010-08-15
7
347 Views
Last Modified: 2013-11-12
Hi All,

I need to obtain the log in information of a user using an asp.net forms authentication log in control and get that information within a silverlight application. How would i do this?

currently once logged in i have:
FormsAuthentication.RedirectFromLoginPage(LoginUser.UserName, true);
FormsAuthentication.SetAuthCookie(LoginUser.UserName, false);

I do not want to use query string as i am using forms authentication and was now wondering how to access the UserName from the cookie within the silverlight application

thanks

baz
0
Comment
Question by:baz86
  • 4
  • 2
7 Comments
 
LVL 22

Expert Comment

by:Mrunal
ID: 33442973
0
 
LVL 8

Expert Comment

by:WesWilson
ID: 33457391
Profile.UserName gives you read access to the logged-in user's UserName.
0
 

Author Comment

by:baz86
ID: 33457849
Hi WesWilson, I can't get access to Profile.UserName from my silverlight app. The actual log in is done using asp.net controls but I want to be able to access the signed in user and use that user info as parameters in my silverlight controls.

Do you have any idea on how i could do this?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 8

Accepted Solution

by:
WesWilson earned 500 total points
ID: 33458061
You can store your username in the Session:

Session.Add("UserName", LoginUser.UserName);

And then to access it from Silverlight, there are three different options mentioned here:
http://www.dotnetspider.com/tutorials/Silverlight-Tutorial-316.aspx

A tutorial on the best option (option 3) is available here: http://www.codeproject.com/KB/silverlight/SilverlightASPNetSessions.aspx
0
 

Author Comment

by:baz86
ID: 33459386
I have looked at the suggested link and tried it by creating a session once a user logs in :

FormsAuthentication.RedirectFromLoginPage(LoginUser.UserName, true);
FormsAuthentication.SetAuthCookie(LoginUser.UserName, false);
Session["userinformation"] = LoginUser.UserName;  
and then create a wcf data service that returns the session information like so:

public object getSessionDetails()
{
return System.Web.HttpContext.Current.Session["userinformation"];
}
 
but I get a NullReferenceException: Object reference not set to an instance of an object.
Any thoughts on how i can get this working?
0
 

Author Comment

by:baz86
ID: 33459935
Hi,
 
In the _service.svc.cs file I put

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
and this seemed to fix the error that I was getting that the state was not getting preserved. But now I have the issue that none of the [OperationContracts] are executing. If i comment out the CompatibilityRequirements, the WCF web methods seem to run ok but i get the same NullReference error in the web method that gets the session details.
I feel im so close to using sessionand also running the WCF queries using the information from the session, but this last bit seems to be hindering me. Could you please help.
Thanks!
0
 

Author Closing Comment

by:baz86
ID: 33468928
This was the correct solution for the question. I never thought of using sessions and seems like using it to access userinformation using sessions is the best way
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question