Custom Identity using MVC5 and OWIN

后端 未结 4 1818
轻奢々
轻奢々 2021-01-30 11:33

I trying to add custom properties to the ApplicationUser for a web site using MVC5 and OWIN authentication. I\'ve read https://stackoverflow.com/a/10524305/264607 and I like ho

4条回答
  •  北海茫月
    2021-01-30 12:15

    I've had the same error.

    My problem was that with anonymous users I wasn't setting the IIdentity on IPrincipal. I did this only when users logged in with user name. Otherwise, IIdentity was null.

    My solution was to always set IIdentity. If user is not authenticated (anonymous user) then IIdentity.IsAuthenticated is set to false. Otherwise, true.

    My code:

    private PrincipalCustom SetPrincipalIPAndBrowser()
    {
         return new PrincipalCustom
         {
           IP = RequestHelper.GetIPFromCurrentRequest(HttpContext.Current.Request),
           Browser = RequestHelper.GetBrowserFromCurrentRequest(HttpContext.Current.Request),
    
        /* User is not authenticated, but Identity must be set anyway. If not, error occurs */
           Identity = new IdentityCustom { IsAuthenticated = false }
         };
    }
    

提交回复
热议问题