【发布时间】:2016-09-02 12:16:44
【问题描述】:
我有一个以前允许匿名访问的 SPA ASP.NET WebAPI 应用程序。我现在已经为其配置了 ASP.Net 身份,但我无法让身份相关的控制器和我的应用程序的其他控制器同时工作:-( 要么是一个,要么是另一个!
我已将启动类添加到我的项目中:
using Test.MyProject;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.DataHandler.Encoder;
using Microsoft.Owin.Security.OAuth;
using Newtonsoft.Json.Serialization;
using Owin;
using System;
using System.Configuration;
using System.Linq;
using System.Net.Http.Formatting;
using System.Web.Http;
[assembly: OwinStartup(typeof(Test.Client.Startup))]
namespace Test.Client
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration httpConfig = new HttpConfiguration();
ConfigureOAuthTokenGeneration(app);
ConfigureWebApi(httpConfig);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
//app.UseWebApi(httpConfig); // If this line is commented out my application's controllers work. But then my Account Controller does't work. It if is included, my application's controllers don't work, whilst the Account Controller work
GlobalConfiguration.Configure(WebApiConfig.Register);
}
private void ConfigureOAuthTokenGeneration(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
}
private void ConfigureWebApi(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
}
}
我还添加了用于管理用户和角色的控制器。
声明 GlobalConfiguration.Configure(WebApiConfig.Register) 之前在 global.aspx.cs 的应用程序启动事件中,但现在移至启动类以将所有内容放在同一位置。
WebApiConfig.Register 方法如下所示:
public static void Register(HttpConfiguration config)
{
var container = new UnityContainer();
// Web API configuration and services
string appStorageProvider = ConfigurationManager.AppSettings["StorageProvider"];
var provider =(TestComposition.StorageProvider) Enum.Parse(typeof (TestComposition.StorageProvider), appStorageProvider, true);
TestComposition.Setup(container, provider);
container.RegisterType<GeneralLogger, GeneralLogger>();
container.RegisterType<IExceptionLogger, ExceptionLogger>();
config.EnableCors();
config.DependencyResolver = new UnityResolver(container);
config.Services.Add(typeof (IExceptionLogger), container.Resolve<GeneralLogger>());
// Web API routes
config.MapHttpAttributeRoutes();
}
在我的新 AccountController 中,我的代码允许我从 Startup 类中设置的 OwinContext 中检索 ApplicationUserManager。
protected ApplicationUserManager AppUserManager
{
get
{
return _AppUserManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
}
使用app.UseWebApi(httpConfig) 注释掉如上所示,我的应用程序可以正常工作。但是,如果我对我的新 AccountController 调用任何操作,我会得到:
Request.GetOwinContext() 错误 CS1061: 'HttpRequestMessage' 没有 包含“GetOwinContext”的定义并且没有扩展方法 'GetOwinContext' 接受类型的第一个参数 可以找到“HttpRequestMessage”(您是否缺少 using 指令 还是程序集参考?)
如果我在app.UseWebApi(httpConfig) 语句中发表评论,AccountController 可以工作,但我的其他控制器不工作。在这里我得到了这样的错误:
{ "message": "发生错误。", "exceptionMessage": "尝试创建类型为 'TestController' 的控制器时发生错误。请确保控制器具有 一个无参数的公共构造函数。", "exceptionType": "System.InvalidOperationException", “stackTrace”:“在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage 请求,HttpControllerDescriptor 控制器描述符,类型 控制器类型)\r\n 在 System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage 请求)\r\n 在 System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()", “内部异常”:{ "message": "发生错误。", "exceptionMessage": "类型 'MyProject.Api.TestController' 没有默认构造函数", "exceptionType": "System.ArgumentException", "stackTrace": " 在 System.Linq.Expressions.Expression.New(Type type)\r\n at System.Web.Http.Internal.TypeActivator.Create[TBase](类型 instanceType)\r\n 在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage 请求,类型 controllerType,Func`1& 激活器)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage 请求,HttpControllerDescriptor 控制器描述符,类型 控制器类型)" } }
知道这里发生了什么吗?
【问题讨论】:
标签: asp.net asp.net-web-api asp.net-identity owin