GetOwinContext通过WCF抛出nullreferenceException

时间:2015-06-17 17:22:21

标签: wcf authentication asp.net-identity owin identity

我已经有一个MVC ASP.NET应用程序,我使用ASP.NET身份管理身份验证。

我在应用中创建了一个WCF服务,允许其他应用使用我的应用提供给他们的服务创建新帐户。

当我调用WCF服务时,当服务尝试使用userManager属性时,我从GetOwinContext()获得NullReference。

这是我的WCF服务实施:

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;
    }
}

我该如何解决?

1 个答案:

答案 0 :(得分:0)

WCF不支持OWIN,您可以在此处看到,http://owin.org/#projects

如果您仍想使用OWIN,则必须切换到REST或者如果要使用WCF则删除OWIN