Welcome!

Artificial Intelligence Authors: Zakia Bouachraoui, Liz McMillan, Yeshim Deniz, Elizabeth White, William Schmarzo

RSS Feed Item

Using ASP.NET Authentication in a Web Service with Silverlight

A reader recently asked me to expand on the ASP.NET Authentication + Silverlight concept I started.. Specifically they wanted to do know how they can have a web service that returns different results depending on who calls the service (and what role they are in). 

Here is my quick walk through of adding that capability to the Silverlight+ASP.NET AppServices sample.

First, let's look at the scenario we are trying to enable.  I wanted to add a "Fire Bob" button. Hopefully, no line of business application has a "fire employee" button, but I thought it showed of the concepts well.   Clearly only managers should be able to fire bob, so the button should be grayed out if you are not logged in or logged in but not in the manager role. 

image

Logging as manager enables the button and clicking on it actually does the firing and reports status.  Notice the background color is a user preference of manager. 

image

And when logged in as an employee, the button is grayed out.

image

 

Defining the service

The first step to enabling this is to define a web service that will fire the employee and offer a way to check to see if you can fire the employee. 

In the web project, add a new item and select the new (in Beta2) Silverlight-enabled WCF service

image

Then define the service as such:

using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Web;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = 
AspNetCompatibilityRequirementsMode.Allowed)] public class FireEmployeeService2 { [OperationContract] public void FireEmployee(string employeeName) { if (CanFireEmployees()) { DoTheDirtyWork(employeeName); } } public void DoTheDirtyWork(string employeeName) { //Proceed to fire the employee } [OperationContract] public bool CanFireEmployees() { return HttpContext.Current.User.IsInRole("Management"); } }

Notice we are defining two entry points, one to check (CanFireEmployees) and one the actually fire. Clearly in a real world scenario you'd want a few more checks here and more of a model where you start a workflow..    The key thing here is the call to User.IsInRole().. the great thing here is that logging in on the client means you are also logged in on the server!  So this just works. 

In the Silverlight project, right click and add a service reference.  Hit "Discover" and select the right one.

image

In the login_Competed method, go ahead and check to see if this person can fire the employee

FireEmployeeService2Client fireClient = new FireEmployeeService2Client();
fireClient.CanFireEmployeesAsync();
fireClient.CanFireEmployeesCompleted += 
    new EventHandler<CanFireEmployeesCompletedEventArgs>(fireClient_CanFireEmployeesCompleted);
this.fireButton.IsEnabled = false;

Then when the call completes,simply set the buttons enable status based on the results. 

void fireClient_CanFireEmployeesCompleted(object sender, CanFireEmployeesCompletedEventArgs e)
 {
     this.fireButton.IsEnabled = e.Result;
 }

Then, the actual implementation of the fire button is streight forward, following all the patterns we have setup for this sort of thing. 

private void fireButton_Click(object sender, RoutedEventArgs e)
 {
     this.fireLabel.Text = "Trying to fire...";
     var client = new FireEmployeeService2Client();
     client.FireEmployeeAsync("bob");
     client.FireEmployeeCompleted += new EventHandler<AsyncCompletedEventArgs>(client_FireEmployeeCompleted);
 }

 void client_FireEmployeeCompleted(object sender, AsyncCompletedEventArgs e)
 {

     if (e.Error != null)
         fireLabel.Text = e.Error.ToString();
     else
         fireLabel.Text = "Call Succeed";
 }

Hope that helps!  you can get the full project here

Read the original blog entry...

IoT & Smart Cities Stories
While the focus and objectives of IoT initiatives are many and diverse, they all share a few common attributes, and one of those is the network. Commonly, that network includes the Internet, over which there isn't any real control for performance and availability. Or is there? The current state of the art for Big Data analytics, as applied to network telemetry, offers new opportunities for improving and assuring operational integrity. In his session at @ThingsExpo, Jim Frey, Vice President of S...
@CloudEXPO and @ExpoDX, two of the most influential technology events in the world, have hosted hundreds of sponsors and exhibitors since our launch 10 years ago. @CloudEXPO and @ExpoDX New York and Silicon Valley provide a full year of face-to-face marketing opportunities for your company. Each sponsorship and exhibit package comes with pre and post-show marketing programs. By sponsoring and exhibiting in New York and Silicon Valley, you reach a full complement of decision makers and buyers in ...
Two weeks ago (November 3-5), I attended the Cloud Expo Silicon Valley as a speaker, where I presented on the security and privacy due diligence requirements for cloud solutions. Cloud security is a topical issue for every CIO, CISO, and technology buyer. Decision-makers are always looking for insights on how to mitigate the security risks of implementing and using cloud solutions. Based on the presentation topics covered at the conference, as well as the general discussions heard between sessio...
In his keynote at 18th Cloud Expo, Andrew Keys, Co-Founder of ConsenSys Enterprise, provided an overview of the evolution of the Internet and the Database and the future of their combination – the Blockchain. Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settl...
The Internet of Things is clearly many things: data collection and analytics, wearables, Smart Grids and Smart Cities, the Industrial Internet, and more. Cool platforms like Arduino, Raspberry Pi, Intel's Galileo and Edison, and a diverse world of sensors are making the IoT a great toy box for developers in all these areas. In this Power Panel at @ThingsExpo, moderated by Conference Chair Roger Strukhoff, panelists discussed what things are the most important, which will have the most profound e...
The Jevons Paradox suggests that when technological advances increase efficiency of a resource, it results in an overall increase in consumption. Writing on the increased use of coal as a result of technological improvements, 19th-century economist William Stanley Jevons found that these improvements led to the development of new ways to utilize coal. In his session at 19th Cloud Expo, Mark Thiele, Chief Strategy Officer for Apcera, compared the Jevons Paradox to modern-day enterprise IT, examin...
Rodrigo Coutinho is part of OutSystems' founders' team and currently the Head of Product Design. He provides a cross-functional role where he supports Product Management in defining the positioning and direction of the Agile Platform, while at the same time promoting model-based development and new techniques to deliver applications in the cloud.
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
LogRocket helps product teams develop better experiences for users by recording videos of user sessions with logs and network data. It identifies UX problems and reveals the root cause of every bug. LogRocket presents impactful errors on a website, and how to reproduce it. With LogRocket, users can replay problems.
Data Theorem is a leading provider of modern application security. Its core mission is to analyze and secure any modern application anytime, anywhere. The Data Theorem Analyzer Engine continuously scans APIs and mobile applications in search of security flaws and data privacy gaps. Data Theorem products help organizations build safer applications that maximize data security and brand protection. The company has detected more than 300 million application eavesdropping incidents and currently secu...