question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AbpSession not loggedin using WinForm

See original GitHub issue
  • Abp 2.3.0 + AbpZero
  • .Net Framework
  • User did not login

I have an issue using abp with winform. I create a form to login and I’m able to login

var loginResult = await GetLoginResultAsync(userTxt.Text, passTxt.Text, "Default");
// The call is below
private async Task<AbpLoginResult<Tenant, User>> GetLoginResultAsync(string usernameOrEmailAddress, string password, string tenancyName)
        {
            Globals.LoginResult = await _logInManager.LoginAsync(usernameOrEmailAddress, password, tenancyName);

The problem is that my AbpSession is still not logged in. Looking on web mobule I see that I need to set the IPrincipalAccessor and I try to set it with admin data but not working

 Configuration.ReplaceService<IPrincipalAccessor, WinFormPrincipalAccessor>(DependencyLifeStyle.Transient);
            //Configuration.ReplaceService<IPrincipalAccessor, HttpContextPrincipalAccessor>(DependencyLifeStyle.Transient);

// My Principal
    public class WinFormPrincipalAccessor : DefaultPrincipalAccessor
    {
        private ClaimsPrincipal _princ;

        public override ClaimsPrincipal Principal
        {
            get
            {
                var id = new GenericIdentity("admin");
                id.AddClaim(new Claim(AbpClaimTypes.TenantId, "1"));
                id.AddClaim(new Claim("nameidentifier","2"));
                id.AddClaim(new Claim("name", "admin"));


                var role = new string[1] { "Admin" };
                _princ = new GenericPrincipal(id, role);
                return _princ;
            }
        }

        public void SetPrincipal(object user)
        {
            
        }
    }

So how can I fill my Principal data like HttpContext for Web? Is it a bug or missing somenthing?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rqx110commented, Sep 26, 2017

after var loginResult = await GetLoginResultAsync(userTxt.Text, passTxt.Text, "Default"); just set Thread.CurrentPrincipal = new ClaimsPrincipal(loginResult.Identity); and then you can use AbpSession as normal . ps: no need to define “WinFormPrincipalAccessor”, just use buildin “DefaultPrincipalAccessor”

0reactions
andmattiacommented, Oct 6, 2017

@rqx110 I try to use your solution but I need to configure

IocManager.IocContainer.Register(
                Component
                    .For<IAuthenticationManager>()
                    .UsingFactoryMethod(() => new WinFormAuthenticationManager(new OwinContext()))
                    .LifestyleTransient()
            );

in web module I use this

            IocManager.IocContainer.Register(
                Component
                    .For<IAuthenticationManager>()
                    .UsingFactoryMethod(() =>
                    {
                                return HttpContext.Current.GetOwinContext().Authentication;
                    })
                    .LifestyleTransient()
                );

in win form I need to init the IAuthenticationManager

Read more comments on GitHub >

github_iconTop Results From Across the Web

AbpSession.UserId is null right after login
Then I configured it as default IAbpSession in MyApp.Console module. Here it is the code: [DependsOn( typeof(MyAppApplicationModule), typeof( ...
Read more >
[Solved]-AbpSession.UserId is null right after login-C#
tenantid and loginresult.user.id . you can also override the current session values with the logged-in user: var loginresult = await _loginmanager.loginasync(..
Read more >
How do I create a login session in C#? - CodeProject
There is no such thing as a "session" in WinForms: it's a construct that is needed in websites because you may have multiple...
Read more >
[Solved] Session in Windows Form
I am building a desktop application.I have made a log in form. First i login to a user and than without log out...
Read more >
Overriding Current Session Values
IAbpSession is also fully integrated and used by other structures in ASP. ... or null if there is no current tenant (in case...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found