You've already forked linux-packaging-mono
acceptance-tests
data
debian
docs
external
ikvm-native
libgc
llvm
m4
man
mcs
build
class
Accessibility
Commons.Xml.Relaxng
Cscompmgd
CustomMarshalers
Facades
I18N
IBM.Data.DB2
ICSharpCode.SharpZipLib
Microsoft.Build
Microsoft.Build.Engine
Microsoft.Build.Framework
Microsoft.Build.Tasks
Microsoft.Build.Utilities
Microsoft.CSharp
Microsoft.NuGet.Build.Tasks
Microsoft.VisualC
Microsoft.Web.Infrastructure
MicrosoftAjaxLibrary
Mono.Btls.Interface
Mono.C5
Mono.CSharp
Mono.Cairo
Mono.Cecil
Mono.Cecil.Mdb
Mono.CodeContracts
Mono.CompilerServices.SymbolWriter
Mono.Data.Sqlite
Mono.Data.Tds
Mono.Debugger.Soft
Mono.Http
Mono.Management
Mono.Messaging
Mono.Messaging.RabbitMQ
Mono.Options
Mono.Parallel
Mono.Posix
Mono.Profiler.Log
Mono.Runtime.Tests
Mono.Security
Mono.Security.Win32
Mono.Simd
Mono.Tasklets
Mono.WebBrowser
Mono.XBuild.Tasks
Novell.Directory.Ldap
PEAPI
RabbitMQ.Client
SMDiagnostics
System
System.ComponentModel.Composition.4.5
System.ComponentModel.DataAnnotations
System.Configuration
System.Configuration.Install
System.Core
System.Data
System.Data.DataSetExtensions
System.Data.Entity
System.Data.Linq
System.Data.OracleClient
System.Data.Services
System.Data.Services.Client
System.Deployment
System.Design
System.DirectoryServices
System.DirectoryServices.Protocols
System.Drawing
System.Drawing.Design
System.Dynamic
System.EnterpriseServices
System.IO.Compression
System.IO.Compression.FileSystem
System.IdentityModel
System.IdentityModel.Selectors
System.Json
System.Json.Microsoft
System.Management
System.Messaging
System.Net
System.Net.Http
System.Net.Http.Formatting
System.Net.Http.WebRequest
System.Net.Http.WinHttpHandler
System.Numerics
System.Numerics.Vectors
System.Reactive.Core
System.Reactive.Debugger
System.Reactive.Experimental
System.Reactive.Interfaces
System.Reactive.Linq
System.Reactive.Observable.Aliases
System.Reactive.PlatformServices
System.Reactive.Providers
System.Reactive.Runtime.Remoting
System.Reactive.Windows.Forms
System.Reactive.Windows.Threading
System.Reflection.Context
System.Runtime.Caching
System.Runtime.CompilerServices.Unsafe
System.Runtime.DurableInstancing
System.Runtime.Remoting
System.Runtime.Serialization
System.Runtime.Serialization.Formatters.Soap
System.Security
System.ServiceModel
System.ServiceModel.Activation
System.ServiceModel.Discovery
System.ServiceModel.Internals
System.ServiceModel.Routing
System.ServiceModel.Web
System.ServiceProcess
System.Threading.Tasks.Dataflow
System.Transactions
System.Web
System.Web.Abstractions
System.Web.ApplicationServices
System.Web.DynamicData
System.Web.Extensions
System.Web.Extensions.Design
System.Web.Http
System.Web.Http.SelfHost
System.Web.Http.WebHost
System.Web.Mobile
System.Web.Mvc3
System.Web.Razor
System.Web.RegularExpressions
System.Web.Routing
System.Web.Services
System.Web.WebPages
System.Web.WebPages.Deployment
System.Web.WebPages.Razor
System.Windows
System.Windows.Forms
System.Windows.Forms.DataVisualization
System.Workflow.Activities
System.Workflow.ComponentModel
System.Workflow.Runtime
System.XML
System.Xaml
System.Xml.Linq
System.Xml.Serialization
SystemWebTestShim
WebMatrix.Data
WindowsBase
aot-compiler
corlib
dlr
doc
legacy
lib
monodoc
notes
reference-assemblies
referencesource
SMDiagnostics
System
System.Activities
System.Activities.Core.Presentation
System.Activities.DurableInstancing
System.Activities.Presentation
System.ComponentModel.DataAnnotations
System.Configuration
System.Core
System.Data
System.Data.DataSetExtensions
System.Data.Entity
System.Data.Entity.Design
System.Data.Linq
System.Data.SqlXml
System.IdentityModel
System.IdentityModel.Selectors
System.Net
System.Numerics
System.Runtime.Caching
System.Runtime.DurableInstancing
System.Runtime.Serialization
System.ServiceModel
System.ServiceModel.Activation
System.ServiceModel.Activities
System.ServiceModel.Channels
System.ServiceModel.Discovery
System.ServiceModel.Internals
System.ServiceModel.Routing
System.ServiceModel.WasHosting
System.ServiceModel.Web
System.Web
System.Web.ApplicationServices
System.Web.DataVisualization
System.Web.DynamicData
System.Web.Entity
System.Web.Entity.Design
System.Web.Extensions
ApplicationServices
ApplicationServiceHelper.cs
ApplicationServicesHostFactory.cs
AuthenticatingEventArgs.cs
AuthenticationService.cs
CreatingCookieEventArgs.cs
KnownTypesProvider.cs
ProfilePropertyMetadata.cs
ProfileService.cs
RoleService.cs
SelectingProviderEventArgs.cs
ValidatingPropertiesEventArgs.cs
ClientServices
Compilation
Configuration
DynamicData
Globalization
Handlers
Management
Profile
Properties
Resources
Script
Security
Util
ui
HttpResponseInternalBase.cs
HttpResponseInternalWrapper.cs
System.Web.Mobile
System.Web.Routing
System.Web.Services
System.Workflow.Activities
System.Workflow.ComponentModel
System.Workflow.Runtime
System.WorkflowServices
System.Xaml.Hosting
System.Xml
System.Xml.Linq
XamlBuildTask
mscorlib
LICENSE.txt
PATENTS.TXT
README.Mono.md
README.md
test-helpers
LICENSE
Makefile
Open.snk
README
ecma.pub
mono.pub
mono.snk
msfinal.pub
reactive.pub
silverlight.pub
winfx.pub
winfx3.pub
docs
errors
ilasm
jay
mcs
packages
tests
tools
AUTHORS
COPYING
INSTALL.txt
Makefile
MonoIcon.png
README
ScalableMonoIcon.svg
mkinstalldirs
mk
mono
msvc
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
196 lines
8.5 KiB
C#
196 lines
8.5 KiB
C#
![]() |
//------------------------------------------------------------------------------
|
||
|
// <copyright file="AuthenticationService.cs" company="Microsoft">
|
||
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||
|
// </copyright>
|
||
|
//------------------------------------------------------------------------------
|
||
|
|
||
|
namespace System.Web.ApplicationServices {
|
||
|
using System;
|
||
|
using System.Diagnostics.CodeAnalysis;
|
||
|
using System.ServiceModel;
|
||
|
using System.ServiceModel.Activation;
|
||
|
using System.Web;
|
||
|
using System.Web.Management;
|
||
|
using System.Web.Resources;
|
||
|
using System.Web.Security;
|
||
|
|
||
|
/// <devdoc>
|
||
|
/// Implements login service contract to be exposed as a WCF service. Uses Membership provider
|
||
|
/// or custom authentication login in the Authenticating event. Also uses Forms.SetAuthCookie() or
|
||
|
/// custom cookie generation via the CreatingCookie event.
|
||
|
/// </devdoc>
|
||
|
|
||
|
[
|
||
|
AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required),
|
||
|
ServiceContract(Namespace="http://asp.net/ApplicationServices/v200"),
|
||
|
ServiceBehavior(Namespace="http://asp.net/ApplicationServices/v200", InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)
|
||
|
]
|
||
|
public class AuthenticationService {
|
||
|
|
||
|
/// <devdoc>
|
||
|
/// Raised to authenticate the user . The event handler sets the e.AuthenticationIsComplete flag to true
|
||
|
/// and e.Authenticated to the result.
|
||
|
/// </devdoc>
|
||
|
private static object _authenticatingEventHandlerLock = new object();
|
||
|
private static EventHandler<AuthenticatingEventArgs> _authenticating;
|
||
|
public static event EventHandler<AuthenticatingEventArgs> Authenticating {
|
||
|
add {
|
||
|
lock (_authenticatingEventHandlerLock) {
|
||
|
_authenticating += value;
|
||
|
}
|
||
|
}
|
||
|
remove {
|
||
|
lock (_authenticatingEventHandlerLock) {
|
||
|
_authenticating -= value;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <devdoc>
|
||
|
/// Raised to create and set the cookie. The event handler shouldset the e.CookieIsSet flag to true, if it is
|
||
|
/// setting the cookie.
|
||
|
/// </devdoc>
|
||
|
private static object _creatingCookieEventHandlerLock = new object();
|
||
|
private static EventHandler<CreatingCookieEventArgs> _creatingCookie;
|
||
|
public static event EventHandler<CreatingCookieEventArgs> CreatingCookie {
|
||
|
add {
|
||
|
lock (_creatingCookieEventHandlerLock) {
|
||
|
_creatingCookie += value;
|
||
|
}
|
||
|
}
|
||
|
remove {
|
||
|
lock (_creatingCookieEventHandlerLock) {
|
||
|
_creatingCookie -= value;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public AuthenticationService() {
|
||
|
}
|
||
|
|
||
|
/// <devdoc>
|
||
|
/// Raises the AuthentincatingEvent if atleast one handler is assigned.
|
||
|
/// </devdoc>
|
||
|
private void OnAuthenticating(AuthenticatingEventArgs e) {
|
||
|
EventHandler<AuthenticatingEventArgs> handler = _authenticating;
|
||
|
if (null != handler) {
|
||
|
handler(this, e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <devdoc>
|
||
|
/// Raises the CreatingCookieEvent if atleast one handler is assigned.
|
||
|
/// </devdoc>
|
||
|
private void OnCreatingCookie(CreatingCookieEventArgs e) {
|
||
|
EventHandler<CreatingCookieEventArgs> handler = _creatingCookie;
|
||
|
if (null != handler) {
|
||
|
handler(this, e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <devdoc>
|
||
|
/// Validates user credentials,without actually setting the FormAuth cookie
|
||
|
/// </devdoc>
|
||
|
/// <param name="username">Username of the account</param>
|
||
|
/// <param name="password">Password of the account</param>
|
||
|
/// <param name="customCredential">Any misc. string to be used by custom authentication logic</param>
|
||
|
/// <returns>True, if credentials are valid, otherwise false</returns>
|
||
|
[OperationContract]
|
||
|
[SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId="username", Justification="consistent with Whidbey")]
|
||
|
public bool ValidateUser(string username, string password, string customCredential) {
|
||
|
ApplicationServiceHelper.EnsureAuthenticationServiceEnabled(HttpContext.Current, true);
|
||
|
return LoginInternal(username, password, customCredential, false, false);
|
||
|
}
|
||
|
|
||
|
/// <devdoc>
|
||
|
/// Validates user credentials,and sets the FormAuth cookie if the credentials are valid.
|
||
|
/// </devdoc>
|
||
|
/// <param name="username">Username of the account</param>
|
||
|
/// <param name="password">Password of the account</param>
|
||
|
/// <param name="customCredential">Any misc. string to be used by custom authentication logic</param>
|
||
|
/// <param name="isPersistent">If true the persistant cookie is generated. </param>
|
||
|
/// <returns>True, if credentials are valid, otherwise false</returns>
|
||
|
[OperationContract]
|
||
|
[SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId="username", Justification="consistent with Whidbey")]
|
||
|
public bool Login(string username, string password, string customCredential, bool isPersistent) {
|
||
|
ApplicationServiceHelper.EnsureAuthenticationServiceEnabled(HttpContext.Current, true);
|
||
|
return LoginInternal(username, password, customCredential, isPersistent, true);
|
||
|
}
|
||
|
|
||
|
/// <devdoc>
|
||
|
/// Checks whether the Forms Authentication cookie attached to the request is valid.
|
||
|
/// </devdoc>
|
||
|
[OperationContract]
|
||
|
public bool IsLoggedIn() {
|
||
|
ApplicationServiceHelper.EnsureAuthenticationServiceEnabled(HttpContext.Current, true);
|
||
|
return HttpContext.Current.User.Identity.IsAuthenticated;
|
||
|
}
|
||
|
|
||
|
/// <devdoc>
|
||
|
/// Clears the Forms Authentication cookie
|
||
|
/// </devdoc>
|
||
|
[OperationContract]
|
||
|
public void Logout() {
|
||
|
ApplicationServiceHelper.EnsureAuthenticationServiceEnabled(HttpContext.Current, false);
|
||
|
FormsAuthentication.SignOut();
|
||
|
}
|
||
|
|
||
|
/// <devdoc>
|
||
|
/// Validates the user credentials.
|
||
|
/// </devdoc>
|
||
|
/// <param name="username"></param>
|
||
|
/// <param name="password"></param>
|
||
|
/// <param name="customCredential"></param>
|
||
|
/// <param name="isPersistent"></param>
|
||
|
/// <param name="setCookie">If this is true, CreatingCookie event is raised, and cookie is set in HttpResponse</param>
|
||
|
/// <returns></returns>
|
||
|
private bool LoginInternal(string username, string password, string customCredential, bool isPersistent, bool setCookie) {
|
||
|
if (null == username) {
|
||
|
throw new ArgumentNullException("username");
|
||
|
}
|
||
|
|
||
|
if (null == password) {
|
||
|
throw new ArgumentNullException("password");
|
||
|
}
|
||
|
AuthenticatingEventArgs authEventArgs = new AuthenticatingEventArgs(username, password, customCredential);
|
||
|
try {
|
||
|
OnAuthenticating(authEventArgs);
|
||
|
|
||
|
if (!authEventArgs.AuthenticationIsComplete) {
|
||
|
MembershipValidate(authEventArgs);
|
||
|
}
|
||
|
if (!authEventArgs.Authenticated) {
|
||
|
Logout();
|
||
|
}
|
||
|
if (authEventArgs.Authenticated && setCookie) {
|
||
|
CreatingCookieEventArgs cookieEventArgs = new CreatingCookieEventArgs(username, password, isPersistent, customCredential);
|
||
|
OnCreatingCookie(cookieEventArgs);
|
||
|
if (!cookieEventArgs.CookieIsSet) {
|
||
|
SetCookie(username, isPersistent);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (Exception e) {
|
||
|
LogException(e);
|
||
|
throw;
|
||
|
}
|
||
|
return authEventArgs.Authenticated;
|
||
|
}
|
||
|
|
||
|
|
||
|
private static void MembershipValidate(AuthenticatingEventArgs e) {
|
||
|
e.Authenticated = Membership.ValidateUser(e.UserName, e.Password);
|
||
|
}
|
||
|
|
||
|
private static void SetCookie(string username, bool isPersistent) {
|
||
|
FormsAuthentication.SetAuthCookie(username, isPersistent);
|
||
|
}
|
||
|
|
||
|
private void LogException(Exception e) {
|
||
|
WebServiceErrorEvent errorevent = new WebServiceErrorEvent(AtlasWeb.UnhandledExceptionEventLogMessage, this, e);
|
||
|
errorevent.Raise();
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|