GetOwinContext through WCF throw nullreferenceException

642 views Asked by At

i already have an MVC ASP.NET application where I manage authentication, using ASP.NET Identity for that.

I created a WCF service in the App, to allow other applications create new accounts using the service that my app provide to them.

When i call the WCF service, i get a NullReference from GetOwinContext() when service try to use userManager property.

This is my WCF Service Implementation:

using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using MyCompany.MyProject.Core.Security.Auth;
using MyCompany.MyProject.Core.Security.Auth.Models;
using MyCompany.MyProject.MvcWebHost.Services.Contracts;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Host.SystemWeb;
using System.ServiceModel;

public class AuthenticationService : IAuthenticationService
{
    private ApplicationUserManager _userManager;
    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

    public OperationResultDTO CreateUserAccount(UserAccountDTO userDto)
    {
        OperationResultDTO result = new OperationResultDTO();

        var user = new ApplicationUser();
        user.UserName = userDto.Identifier.ToString();
        user.Email = userDto.Email;
        Task<IdentityResult> adminresult = UserManager.CreateAsync(user, userDto.Password);

        if (adminresult.IsCompleted && adminresult.IsFaulted != false)
        {
            result.IsSuccess = true;
            result.HasError = false;
        }
        else
        {
            result.IsSuccess = false;
            result.HasError = true;
            result.ErrorMessage = "This is an error message!";
        }

        return result;
    }
}

How can i solve it?

1

There are 1 answers

1
matthijsb On

OWIN is not supported with WCF as you can see here, http://owin.org/#projects

If you still want to use OWIN you have to switch to REST or drop OWIN if you want to use WCF