Showing posts with label Silverlight. Show all posts
Showing posts with label Silverlight. Show all posts

2008-09-08

User authentication in SilverLight

I wanted to know how to authenticate users in a SilverLight app using their Windows login info.

01: Set the authentication mode to Windows and <deny users="?"/> in <system.web> within web.config
02: Move the silverlight control to Default.aspx and set that as your start page
03: Add the following Page_Load code

  protected void Page_Load(object sender, EventArgs e)
{
IPrincipal p = HttpContext.Current.User;
if (p == null)
throw new SecurityException("No current user");
if (!(p is WindowsPrincipal))
throw new SecurityException("Not a windows user");
if (!p.Identity.IsAuthenticated)
throw new SecurityException("Not authenticated");
Xaml1.InitParameters =
string.Format("user={0},session={1}", p.Identity.Name, Session.SessionID);
}


04: In app.xaml.cs you can now read the InitParameters using e.InitParameters in the Application_Startup method.

2008-04-03

Silverlight and webservices

First download the binaries you need from here:
http://silverlight.net/GetStarted/

Next run VS2008 and create a new project.

Select the Silverlight node and then the Silverlight Application node.

ProjectName = MySilverlightApp
Tick the checkbox "Create directory for solution"
Click OK

On the wizard page you want the default values:
* Add a new page to the solution for hosting the control
Project type = Web Site
Name = MyWebService

Now delete the two ASPX files, we wont be needing those.

Rename the HTML page to Index.html and set it as the project start page.

Right click the website project and select "Add new item".

Select "Web Service".
Name = DateTimeService.asmx
Click ADD

Change the HelloWorld method to

public DateTime GetServerDateTime()
{
  return DateTime.Now;
}


Right-click the References node on the Silverlight project and select "Add service reference".
Click the "Discover" button, and in the tree view that appears select the DateTimeServer node.
Set the NameSpace to DateTimeServer
Click OK

Now open Page.xaml and enter the following within the <Grid>

<StackPanel>
  <Button x:Name="ButtonGetServerDateTime" Content="Get date time" Click="ButtonGetServerDateTime_Click"/>
  <TextBlock x:Name="TextBlockDateTime" Text="Ready..."/>
</StackPanel>


Note: When you type Click=" you will get the option to hit <TAB> to implement the event handler.

Now open Page.xaml.cs

Add the following private member to the class:
private DateTimeServer.DateTimeServiceSoapClient proxy;


Initialise it in the page’s constructor:
proxy = new MySilverlightApp.DateTimeServer.DateTimeServiceSoapClient();
proxy.GetServerDateTimeCompleted +=
new EventHandler<MySilverlightApp.DateTimeServer.GetServerDateTimeCompletedEventArgs>(proxy_GetServerDateTimeCompleted);


Implement the "Completed" method like so:
TextBlockDateTime.Text = e.Result.ToString();


Finally implement the ButtonServerDateTime_Click method like so:
private void GetServerDateTime_Click(object sender, RoutedEventArgs e)
{
  proxy.GetServerDateTimeAsync();
}


That’s it! Run the app and click the button!