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

.NET 7 - How to manage authentication ? #445

Closed
KumG opened this issue Oct 12, 2023 · 4 comments
Closed

.NET 7 - How to manage authentication ? #445

KumG opened this issue Oct 12, 2023 · 4 comments

Comments

@KumG
Copy link

KumG commented Oct 12, 2023

Hello,

Sorry if this isn't the right place to ask this, but I didn't find a sample or documentation to manage the authentication with .NET 6/7...

I'm trying to migrate my project from .NET 4.8 to .NET 7. (WPF client, ASP.NET web with EF 6.4, OpenRIAServices)

My current AuthenticationDomainService inherits from AuthenticationBase<User> from OpenRiaServices.Server.Authentication.AspNetMembership package.

It uses FormsAuthentication and this package is not compatible with .NET 7.

I tried to replace it with IAuthentication<User> with a login method :

public User Login(string userName, string password, bool isPersistent, string customData)
{
	var user = RetrieveUser("admin");

	// Set HttpContext current user ?

	return user;
}

In my client test application, I have this code :

DomainContext.DomainClientFactory = new BinaryHttpDomainClientFactory(new Uri("http://localhost:5241/", UriKind.Absolute), () => new HttpClient());

var webContext = new WebContext { Authentication = new FormsAuthentication { DomainContext = new AuthenticationDomainContext() } };

...

var authenticationService = WebContextBase.Current.Authentication;

authenticationService.Login(new LoginParameters("admin", "12345"), operation =>
            {
                var currentUser = WebContextBase.Current.Authentication.User;
                var otherDomainContext = new SampleDomainContext();
                var query = otherDomainContext.GetAccessProfilesQuery(false);
                otherDomainContext.Load(query, LoadBehavior.RefreshCurrent, loadOperation =>
                {

                }, null);
            }, null);

My domainContext method has [RequiresAuthentication]

Of course, I always get a UnauthorizedAccessException...

Thank you.

@KumG KumG added the bug label Oct 12, 2023
@Daniel-Svensson
Copy link
Member

Server

On the server you need to setup authentication using "normal" aspnetcore practices.

It looks like you want to use cookie based authentication which is the clooses match to was AspNetMembership did, you can look into the official AspNetCore documentation in contains instruction for setup, as well as set the cookie on successfull login as well as logout code.

Client

For the client, you need to ensure that all HttpClients share the same CookieContainer and that the it is set to use to cookies (https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclienthandler.usecookies?view=net-7.0#system-net-http-httpclienthandler-usecookies), you can do something similar to

DomainContext.DomainClientFactory = new BinaryHttpDomainClientFactory(TestURIs.RootURI, new HttpClientHandler()
{
CookieContainer = new CookieContainer(),
UseCookies = true,
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
});

@KumG
Copy link
Author

KumG commented Oct 16, 2023

Thank you very much for the help.

I was able to have a working proof of concept using :

        public User Login(string userName, string password, bool isPersistent, string customData)
        {
            var user = RetrieveUser("admin");

            if (user != null)
            {
                var claims = new List<Claim> { new Claim(ClaimTypes.Name, user.Name)};
                claims.AddRange(user.Roles.Select(role => new Claim(ClaimTypes.Role, role)));
                var claimsIdentity = new ClaimsIdentity(claims, "Forms");
                var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
                _httpContextAccessor.HttpContext.SignInAsync(claimsPrincipal).GetAwaiter().GetResult();
            }

            return user;
        }

@KumG KumG closed this as completed Oct 16, 2023
@Daniel-Svensson
Copy link
Member

I've started with a sample at OpenRIAServices/Samples#16 if anybody else is interested.

The login method should probably be rewriten more similar to the above sample by @KumG

@Daniel-Svensson
Copy link
Member

Added documentation in https://github.com/OpenRIAServices/OpenRiaServices/pull/466/files

@KumG feel free to have a look and see if there is anything important I have forgotten

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants