Base solution for your next web application
Open Closed

ABPSession.UserId is NULL after calling SignInAsync #1458


User avatar
0
djfuzzy created

I want to store some user information in the session using claims after they log in but I am having a problem. This user information is in a Client entity that I am passing some info to a ClientDto that I have added to CurrentLoginInformationsOutput. Consider the following code:

public async Task<JsonResult> Login(LoginViewModel loginModel, string returnUrl = "", string returnUrlHash = "")
        {
            ...
            await SignInAsync(loginResult.User, loginResult.Identity, loginModel.RememberMe, loginModel.IsClientLogin);            

            if (loginModel.IsClientLogin)
            {
                var loginInformations = await _sessionAppService.GetCurrentLoginInformations();
                if (loginInformations.Client == null)
                    throw new UserFriendlyException(string.Format("There is no client associated with the user with Id {0}.",
                        loginResult.User.Id));
                // Do something with client information 
            }
            ...
            return Json(new MvcAjaxResponse { TargetUrl = returnUrl });
        }

When _sessionAppService.GetCurrentLoginInformations() is called, I am getting the following exception:

WARN 2016-07-26 15:32:22,220 [44 ] Abp.Logging.LogHelper - Can not find 'CurrentUserDidNotLoginToTheApplication' in localization source 'Abp'! ERROR 2016-07-26 15:32:26,347 [51 ] rtgage.Web.Controllers.AccountController - [CurrentUserDidNotLoginToTheApplication] Abp.Authorization.AbpAuthorizationException: [CurrentUserDidNotLoginToTheApplication] at Abp.Authorization.AuthorizeAttributeHelper.<AuthorizeAsync>d__13.MoveNext() in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Authorization\AuthorizeAttributeHelper.cs:line 29 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException(Task task) at Nito.AsyncEx.AsyncContext.<>c__DisplayClass3.<Run>b__1(Task t) at System.Threading.Tasks.ContinuationTaskFromTask.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException(Task task) at Nito.AsyncEx.AsyncContext.Run(Func1 action) at Abp.Threading.AsyncHelper.RunSync(Func1 action) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Threading\AsyncHelper.cs:line 42 at Abp.Authorization.AuthorizeAttributeHelper.Authorize(IEnumerable1 authorizeAttributes) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Authorization\AuthorizeAttributeHelper.cs:line 45 at Abp.Authorization.Interceptors.AuthorizationInterceptor.Authorize(IEnumerable1 authorizeAttributes) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Authorization\Interceptors\AuthorizationInterceptor.cs:line 86 at Abp.Authorization.Interceptors.AuthorizationInterceptor.InterceptSync(IInvocation invocation, IEnumerable1 authorizeAttributes) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Authorization\Interceptors\AuthorizationInterceptor.cs:line 78 at Abp.Authorization.Interceptors.AuthorizationInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Authorization\Interceptors\AuthorizationInterceptor.cs:line 43 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformAsyncUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 62 at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 41 at Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 35 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Auditing.AuditingInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Auditing\AuditingInterceptor.cs:line 52 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Runtime.Validation.Interception.ValidationInterceptor.Intercept(IInvocation invocation) in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Runtime\Validation\Interception\ValidationInterceptor.cs:line 33 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.SessionAppServiceProxy.GetCurrentLoginInformations() at NextMortgage.Web.Controllers.AccountController.<Login>d__12.MoveNext() in C:\Users\Dillon\Source\Repos\NextMortgage\NextMortgage.Web\Controllers\AccountController.cs:line 118 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at System.Threading.Tasks.TaskHelpersExtensions.ThrowIfFaulted(Task task) at System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End() at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag) at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag) at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.

For one thing, I do have 'CurrentUserDidNotLoginToTheApplication' in my localization source. Secondly, how is my user not logged in yet at this point despite the SignInAsync() successfully completing? Any help would be appreciated.


4 Answer(s)
  • User Avatar
    0
    djfuzzy created

    If someone can please help me with this that would be great. I'm struggling to figure out why the AbpSession.UserId is null right after the AuthenticationManager.SignIn. From that SignInAsync method, I need to be able to call an application service method that requires authorization. Also, I would like to know why the proper localization source isn't being used to show the string for [CurrentUserDidNotLoginToTheApplication], which exists in my configured and otherwise working localization source.

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    CurrentUserDidNotLoginToTheApplication should be located in your project's localization source. Have you checked (or searched that string) in your solution?

    We use ASP.NET Identity. Probably it does not set Thread.CurrentPrincipals on signin. I suggest you to create a non authorized version of GetCurrentLoginInformations() and use it with loginResult.User.Id.

  • User Avatar
    0
    djfuzzy created

    Thanks for your help! I'll create a non-authorized service method to use at Sign In.

    About the localized string, it certainly does exist in my localization source. Are you able to reproduce it by trying to call a service method that requires authorization in the Sign In method?

  • User Avatar
    0
    hikalkan created
    Support Team

    You can try this:

    Thread.CurrentPrincipal = new ClaimsPrincipal(identity);
                HttpContext.User = new ClaimsPrincipal(identity);