应用程序服务
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<membership defaultProvider="SampleMembershipProvider">
<providers>
<add name="SampleMembershipProvider" type="AppService.SampleMembershipProvider"/>
</providers>
</membership>
<roleManager enabled="true" defaultProvider="SampleRoleProvider">
<providers>
<add name="SampleRoleProvider" type="AppService.SampleRoleProvider"/>
</providers>
</roleManager>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.web.extensions>
<scripting>
<webServices>
<authenticationService enabled="true"/>
<roleService enabled="true"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
namespace AppService
{
public class SampleMembershipProvider:MembershipProvider
{
private Dictionary<string, string> users = new Dictionary<string, string>();
internal static string ManagerUserName = "Manager".ToLowerInvariant();
internal static string EmployeeUserName = "Employee".ToLowerInvariant();
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
users.Add(ManagerUserName, "secret@Pa$$w0rd");
users.Add(EmployeeUserName, "s0me@Secret");
base.Initialize(name, config);
}
public override string ApplicationName
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public override bool ChangePassword(string username, string oldPassword, string newPassword)
{
throw new NotImplementedException();
}
public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
{
throw new NotImplementedException();
}
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
throw new NotImplementedException();
}
public override bool DeleteUser(string username, bool deleteAllRelatedData)
{
throw new NotImplementedException();
}
public override bool EnablePasswordReset
{
get { throw new NotImplementedException(); }
}
public override bool EnablePasswordRetrieval
{
get { throw new NotImplementedException(); }
}
public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}
public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}
public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}
public override int GetNumberOfUsersOnline()
{
throw new NotImplementedException();
}
public override string GetPassword(string username, string answer)
{
throw new NotImplementedException();
}
public override MembershipUser GetUser(string username, bool userIsOnline)
{
throw new NotImplementedException();
}
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
throw new NotImplementedException();
}
public override string GetUserNameByEmail(string email)
{
throw new NotImplementedException();
}
public override int MaxInvalidPasswordAttempts
{
get { throw new NotImplementedException(); }
}
public override int MinRequiredNonAlphanumericCharacters
{
get { throw new NotImplementedException(); }
}
public override int MinRequiredPasswordLength
{
get { throw new NotImplementedException(); }
}
public override int PasswordAttemptWindow
{
get { throw new NotImplementedException(); }
}
public override MembershipPasswordFormat PasswordFormat
{
get { throw new NotImplementedException(); }
}
public override string PasswordStrengthRegularExpression
{
get { throw new NotImplementedException(); }
}
public override bool RequiresQuestionAndAnswer
{
get { throw new NotImplementedException(); }
}
public override bool RequiresUniqueEmail
{
get { throw new NotImplementedException(); }
}
public override string ResetPassword(string username, string answer)
{
throw new NotImplementedException();
}
public override bool UnlockUser(string userName)
{
throw new NotImplementedException();
}
public override void UpdateUser(MembershipUser user)
{
throw new NotImplementedException();
}
public override bool ValidateUser(string username, string password)
{
if (users.ContainsKey(username.ToLowerInvariant()))
{
return password.Equals(users[username.ToLowerInvariant()]);
}
return false;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
namespace AppService
{
public class SampleRoleProvider:RoleProvider
{
internal static string ManagerRoleName = "Manager".ToLowerInvariant();
internal static string EmployeeRoleName = "Employee".ToLowerInvariant();
public override void AddUsersToRoles(string[] usernames, string[] roleNames)
{
throw new NotImplementedException();
}
public override string ApplicationName
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public override void CreateRole(string roleName)
{
throw new NotImplementedException();
}
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
{
throw new NotImplementedException();
}
public override string[] FindUsersInRole(string roleName, string usernameToMatch)
{
throw new NotImplementedException();
}
public override string[] GetAllRoles()
{
throw new NotImplementedException();
}
public override string[] GetRolesForUser(string username)
{
if (string.Compare(username, SampleMembershipProvider.ManagerUserName, true) == 0)
{
return new string[] { ManagerRoleName };
}
else if (string.Compare(username, SampleMembershipProvider.EmployeeUserName, true) == 0)
{
return new string[] { EmployeeRoleName };
}
else
{
return new string[0];
}
}
public override string[] GetUsersInRole(string roleName)
{
throw new NotImplementedException();
}
public override bool IsUserInRole(string username, string roleName)
{
string[] roles = GetRolesForUser(username);
foreach (var role in roles)
{
if (string.Compare(role, roleName, true) == 0)
{
return true;
}
}
return false;
}
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
{
throw new NotImplementedException();
}
public override bool RoleExists(string roleName)
{
throw new NotImplementedException();
}
}
}
客户端
<Window x:Class="AuthenticationExam.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label VerticalAlignment="Center" Margin="5" Grid.Row="0" Grid.Column="0" Content="Username:" />
<Label VerticalAlignment="Center" Margin="5" Grid.Row="1" Grid.Column="0" Content="Password:" />
<TextBox x:Name="textUsername" Margin="5" Grid.Row="0" Grid.Column="1" />
<PasswordBox x:Name="textPassword" Margin="5" Grid.Row="1" Grid.Column="1" />
<Button x:Name="buttonLogin" Click="OnLogin" Grid.Row="2" Grid.Column="1" Margin="5" Content="Login" />
<TextBlock Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" x:Name="labelValidatedInfo" Visibility="Hidden" Text="User Validated" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace AuthenticationExam
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void OnLogin(object sender, RoutedEventArgs e)
{
try
{
labelValidatedInfo.Visibility = Visibility.Hidden;
if (Membership.ValidateUser(textUsername.Text,
textPassword.Password))
{
// user validated!
labelValidatedInfo.Visibility = Visibility.Visible;
}
else
{
MessageBox.Show("Username or password not valid",
"Client Authentication Services", MessageBoxButton.OK,
MessageBoxImage.Warning);
}
}
catch (WebException ex)
{
MessageBox.Show(ex.Message, "Client Application Services",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="http://localhost:55555/AppServices/Authentication_JSON_AppService.axd" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="http://localhost:55555/AppServices/Role_JSON_AppService.axd" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
</configuration>