Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

Access User Name in application

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
baz86
Asked:
baz86
  • 4
  • 2
1 Solution
 
WesWilsonCommented:
Profile.UserName gives you read access to the logged-in user's UserName.
0
 
baz86Author Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
WesWilsonCommented:
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
 
baz86Author Commented:
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
 
baz86Author Commented:
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
 
baz86Author Commented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now