Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: authentication bug #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SampleCRM.Browser/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:57249;https://localhost:57248",
"applicationUrl": "https://localhost:54845",
"hotReloadEnabled": false
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/SampleCRM.Web/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ protected void Session_Start(object sender, EventArgs e)
}

protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
{
#if RELEASE
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "https://samplecrm-webservices.azurewebsites.net");
#else
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "https://localhost:54845");
#endif
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials", "true");

if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
Expand Down
15 changes: 11 additions & 4 deletions src/SampleCRM.Web/Models/User.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using OpenRiaServices.DomainServices.Server.ApplicationServices;
using OpenRiaServices.DomainServices.Server.Authentication;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace SampleCRM.Web
{
/// <summary>
/// Class containing information about the authenticated user.
/// </summary>
public partial class User : UserBase
public partial class User : IUser
{
//// NOTE: Profile properties can be added for use in Silverlight application.
//// NOTE: Profile properties can be added for use in application.
//// To enable profiles, edit the appropriate section of web.config file.
////
//// public string MyProfileProperty { get; set; }
Expand All @@ -16,5 +18,10 @@ public partial class User : UserBase
/// Gets and sets the friendly name of the user.
/// </summary>
public string FriendlyName { get; set; }

[Key]
public string Name { get; set; }

public IEnumerable<string> Roles { get; set; } = new List<string>();
}
}
}
600 changes: 306 additions & 294 deletions src/SampleCRM.Web/SampleCRM.Web.csproj

Large diffs are not rendered by default.

99 changes: 95 additions & 4 deletions src/SampleCRM.Web/Services/AuthenticationService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using OpenRiaServices.DomainServices.Hosting;
using OpenRiaServices.DomainServices.Server.ApplicationServices;
using OpenRiaServices.DomainServices.Server;
using OpenRiaServices.DomainServices.Server.Authentication;
using System;
using System.Security.Principal;
using System.Web;
using System.Web.Security;

namespace SampleCRM.Web
{
Expand All @@ -17,6 +22,92 @@ namespace SampleCRM.Web
/// Most of the functionality is already provided by the AuthenticationBase class.
/// </summary>
[EnableClientAccess]
public class AuthenticationService : AuthenticationBase<User>
{ }
}
public class AuthenticationService : DomainService, IAuthentication<User>
{
private static readonly User DefaultUser = new User
{
Name = string.Empty
};

/// <summary>
/// Use this method to fill your User object with additional data (from database, for example)
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
private User MapMembershipUser(MembershipUser user)
{
return new User
{
Name = user.UserName
};
}

public bool ValidateUser(string userName, string password) => Membership.ValidateUser(userName, password);

[Query(IsComposable = false)]
public User GetUser()
{
IIdentity identity = null;
try
{
identity = ServiceContext?.User?.Identity;
}
catch (InvalidOperationException)
{

}

if (identity is null) return DefaultUser;

if (identity.IsAuthenticated)
{
var user = Membership.GetUser(identity.Name);

return MapMembershipUser(user);
}

return DefaultUser;
}

public User Login(string userName, string password, bool isPersistent, string customData)
{
if (!ValidateUser(userName, password)) return default;

// if IsPersistent is true, will keep logged in for up to a week (or until you logout)
var ticket = new FormsAuthenticationTicket(
version: 1,
name: userName,
issueDate: DateTime.Now,
expiration: DateTime.Now.AddMinutes(60),
isPersistent,
userData: customData ?? string.Empty,
FormsAuthentication.FormsCookiePath);

var encryptedTicket = FormsAuthentication.Encrypt(ticket);
var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
{
HttpOnly = false,
Expires = ticket.Expiration
};

var httpContext = (HttpContextBase)ServiceContext.GetService(typeof(HttpContextBase));
httpContext.Response.Cookies.Add(authCookie);

var user = Membership.GetUser(userName);

if (user == null) return default;

return MapMembershipUser(user);
}

public User Logout()
{
FormsAuthentication.SignOut();
return DefaultUser;
}

public void UpdateUser(User user)
{
}
}
}
10 changes: 9 additions & 1 deletion src/SampleCRM.Web/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
</system.Web>
-->
<system.web>
<authentication mode="Forms"/>
<authentication mode="Forms">
<forms cookieless="UseCookies" name=".ASPXAUTH"/>
</authentication>
<httpModules>
<add name="DomainServiceModule" type="OpenRiaServices.DomainServices.Hosting.DomainServiceHttpModule, OpenRiaServices.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=2e0b7ccb1ae5b4c8"/>
</httpModules>
Expand Down Expand Up @@ -99,6 +101,12 @@
</behavior>
</serviceBehaviors>
</behaviors>
<domainServices>
<endpoints>
<add name="soap" type="OpenRiaServices.DomainServices.Hosting.SoapXmlEndpointFactory, OpenRiaServices.DomainServices.Hosting.Endpoint"/>
<add name="json" type="OpenRiaServices.DomainServices.Hosting.JsonEndpointFactory, OpenRiaServices.DomainServices.Hosting.Endpoint"/>
</endpoints>
</domainServices>
</system.serviceModel>
<system.data>
<DbProviderFactories>
Expand Down
28 changes: 16 additions & 12 deletions src/SampleCRM.Web/packages.config
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.4.4" targetFramework="net481" />
<package id="Microsoft.AspNet.Providers.Core" version="2.0.0" targetFramework="net461" />
<package id="OpenRiaServices.EntityFramework" version="4.6.0" targetFramework="net461" />
<package id="OpenRiaServices.Server" version="4.6.0" targetFramework="net461" />
<package id="SQLiteMembershipProvider" version="1.1.0.0" targetFramework="net461" />
<package id="Stub.System.Data.SQLite.Core.NetFramework" version="1.0.118.0" targetFramework="net481" />
<package id="System.Data.SQLite" version="1.0.118.0" targetFramework="net481" />
<package id="System.Data.SQLite.Core" version="1.0.118.0" targetFramework="net481" />
<package id="System.Data.SQLite.EF6" version="1.0.118.0" targetFramework="net481" />
<package id="System.Data.SQLite.Linq" version="1.0.118.0" targetFramework="net481" />
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.4.4" targetFramework="net481" />
<package id="Microsoft.AspNet.Providers.Core" version="2.0.0" targetFramework="net461" />
<package id="OpenRiaServices.Endpoints" version="5.0.0-preview0003" targetFramework="net48" />
<package id="OpenRiaServices.EntityFramework" version="5.0.0-preview0003" targetFramework="net48" />
<package id="OpenRiaServices.Server" version="5.0.0-preview0003" targetFramework="net48" />
<package id="OpenRiaServices.Server.Authentication.AspNetMembership" version="5.0.0-preview0003" targetFramework="net48" />
<package id="SQLiteMembershipProvider" version="1.1.0.0" targetFramework="net461" />
<package id="Stub.System.Data.SQLite.Core.NetFramework" version="1.0.118.0" targetFramework="net481" />
<package id="System.Data.SQLite" version="1.0.118.0" targetFramework="net481" />
<package id="System.Data.SQLite.Core" version="1.0.118.0" targetFramework="net481" />
<package id="System.Data.SQLite.EF6" version="1.0.118.0" targetFramework="net481" />
<package id="System.Data.SQLite.Linq" version="1.0.118.0" targetFramework="net481" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net48" />
<package id="System.Threading.Tasks.Extensions" version="4.5.3" targetFramework="net48" />
</packages>
10 changes: 8 additions & 2 deletions src/SampleCRM/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#endif

using System;
using System.Net;
using System.Windows;

namespace SampleCRM
Expand All @@ -20,6 +21,8 @@ public partial class App : Application

public App()
{
Current.Host.Settings.DefaultSoapCredentialsMode = CredentialsMode.Enabled;

Startup += Application_Startup;
UnhandledException += Application_UnhandledException;

Expand All @@ -33,15 +36,18 @@ public App()
#if LOCAL_DEBUG
BaseUrl = "http://localhost:7002/";
#elif DEBUG
BaseUrl = "http://localhost:54837/";
BaseUrl = "https://localhost:44350/";
#elif LOCAL_RELEASE
BaseUrl = "http://localhost:7002/";
#elif RELEASE
BaseUrl = "https://samplecrm-webservices.azurewebsites.net/";
#else
throw new NotSupportedException();
#endif
((DomainClientFactory)DomainContext.DomainClientFactory).ServerBaseUri = new Uri(BaseUrl);
DomainContext.DomainClientFactory = new OpenRiaServices.DomainServices.Client.Web.WebAssemblySoapDomainClientFactory()
{
ServerBaseUri = new Uri(BaseUrl)
};
}

private void Application_Startup(object sender, StartupEventArgs e)
Expand Down
10 changes: 2 additions & 8 deletions src/SampleCRM/SampleCRM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,8 @@
</Content>

<PackageReference Include="OpenSilver" Version="2.1.0-preview-2024-01-06-151852-9c24678f" />
<PackageReference Include="OpenSilver.OpenRiaServices.Client.4.6" Version="2.0.0" />
<PackageReference Include="OpenSilver.OpenRiaServices.Client.Core.4.6" Version="2.0.0" />
<PackageReference Include="OpenSilver.OpenRiaServices.CodeGen.4.6" Version="2.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="OpenSilver.OpenRiaServices.DomainDataSource.4.6" Version="2.0.0" />

<PackageReference Include="OpenSilver.OpenRiaServices.Client" Version="2.0.0" />

<Page Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down