这让我疯了.
我正在使用最新的signalR版本(2.0.2).这是我的集线器代码(OnConnected)
        public override Task OnConnected()
        {
            //User is null then Identity and Name too.
            Connections.Add(Context.User.Identity.Name, Context.ConnectionId);
            return base.OnConnected();
        }
Run Code Online (Sandbox Code Playgroud)
这是我的Controller的登录方法:
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
              var user = await UnitOfWork.UserRepository.FindAsync(model.UserName,  model.Password);
                if (user != null)
                {
                    await SignInAsync(user, model.RememberMe);
                    return RedirectToLocal(returnUrl);
                }
            }
            TempData["ErrorMessage"] = Resources.InvalidUserNameOrPassword;
            // If we got this far, something failed, redisplay form
            return RedirectToAction("Index","Home");
        }
Run Code Online (Sandbox Code Playgroud)
我发现有些人在OnDisconnected上遇到这个问题,我甚至都没有.
我正在使用MCV5模板.
你知道什么是错的吗?
我想在中间件组件中连接异常处理,如下所示:
public override async Task Invoke(IOwinContext context)
{
    try
    {
        await Next.Invoke(context);
    }
    catch (Exception ex)
    {
        // Log error and return 500 response
    }
}
Run Code Online (Sandbox Code Playgroud)
但是,HttpErrorResponse在我可以访问之前,我想要捕获的一些异常被Web API管道捕获并转换为s.在这个过程中,我丢失了很多关于错误的细节,所以在调试时我无法得到有用的堆栈跟踪(调试器在抛出异常时甚至不会停止 - 我必须手动单步执行代码并查看它失败的地方......).
我尝试使用以下实现添加自定义异常处理程序:
public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
{
    var owinContext = context.Request.GetOwinContext();
    owinContext.Set(Constants.ContextKeys.Exception, context.Exception);
    return Task.FromResult(0);
}
Run Code Online (Sandbox Code Playgroud)
通过注册config.Services.Replace(typeof(IExceptionHandler), new MyExceptionHandler());在我的启动配置,但在执行后看着它Next.Invoke(context)通过
context.Get<Exception>(Constants.ContextKeys.Exception);
Run Code Online (Sandbox Code Playgroud)
仍然没有给我所有我想要的细节,以及没有在调试器的故障点停止.
有没有办法可以完全关闭所有内置错误处理,以便我自己的中间件可以处理它?
澄清,因为很多人似乎误解了我的追求:
我使用这些代码片段自我托管OWIN Web API:
class Startup
{
    public void Configuration(IAppBuilder appBuilder)
    {
        var config = new HttpConfiguration();
        var route = config.Routes.MapHttpRoute("DefaultApi", "{controller}");
        appBuilder.UseWebApi(config);
    }
}
WebApp.Start<Startup>("http://localhost:8080")
Run Code Online (Sandbox Code Playgroud)
我想在我的Web API服务关闭时运行一些代码.我正在寻找类似的东西HttpApplication.Application_End,一个Disposed活动,或者一个很好的位置override void Dispose().
如何在Web API服务关闭时运行代码?
自托管OWIN Web API是否可以在非管理员帐户下运行?我已经尝试了几十个网址预订,没有任何作用.该服务无法以"访问被拒绝"开头.当帐户添加到管理员角色但我不想要它时,它可以工作.下面的代码在Win 7 framework 4.5.2上运行.
//install-package microsoft.owin.hosting
//install-package Microsoft.Owin.Host.HttpListener
StartOptions options = new StartOptions();
options.Urls.Add("http://localhost:5000/");
//options.Urls.Add(string.Format("http://{0}:5000", Environment.MachineName));
//options.Urls.Add("http://+:5000/");
//options.Urls.Add("http://*:5000/");
using (WebApp.Start<WebAPISelfHostMinimal.Startup>(options))
{
    while (!Terminate)
    {
        await Task.Delay(10); //keep cpu from getting pegged
    }
    LogUtil.LogInfo("Terminating owin host.");
}
Run Code Online (Sandbox Code Playgroud)
编辑 - 这是在Windows帐户下运行.
C:\>netsh http add urlacl http://+:5000/ user=mini2012\svcAPI
URL reservation successfully added
C:\>sc start apiservice
[SC] StartService FAILED 5:
Access is denied.
C:\>netsh http add urlacl http://*:5000/ user=mini2012\svcAPI
URL reservation successfully added
C:\>sc start apiservice
[SC] StartService FAILED 5:
Access is …Run Code Online (Sandbox Code Playgroud) 我不太明白...确定它能够自我托管一个应用程序很酷,它可能很好,因为如果IIS出于任何原因失败...那么你的所有网站都会崩溃..但如果它们是自我托管然后他们住在那里自己的生活在自己的背景..我猜这很好..但我仍然没有得到这个的优点..我在IIS管道中通过使用owin加速应用程序快速跳过很多不必要的东西,或者......实际的职业选手是什么?(你不需要列出所有这些,如果它们很多:),但我知道你为什么要在IIS上使用OWIN和Katana)
提前致谢!
将第三方登录集成到ASP.NET应用程序的OWIN中间件非常酷,但我似乎无法弄清楚如何从新的ID框架中删除它来取代糟糕的Membership API.我对在基于EF的数据持久性中持久化声明和用户信息不感兴趣,我只想要声明信息,以便我可以将它应用于我自己在现有项目中的用户帐户.我不想采用新的ID框架来利用这些东西.
我一直在浏览CodePlex上的代码,但是有很多静态魔法.你能提出任何建议吗?
在使用OWIN请求管道创建ApplicationUserManager时,使用依赖项注入创建自定义UserStore时遇到问题.
背景
我正在尝试将我们的Web应用程序中的用户功能从使用SimpleMembership迁移到新的ASP.NET标识.在启动新的MVC 5项目时,单页面应用程序的默认实现使用ASP.Identity,使用Entity Framework实现UserStore功能.
在我的例子中,我们已经使用NHibernate作为ORM,并使用ninject实现工作单元模式,以便每个请求有一个NHibernate会话,我想让ASP.Identity与我们现有的框架一起工作.
为此,我创建了一个自定义UserStore,可以通过注入相关的存储库/ nhibernate会话等来创建.然后可以使用Ninject将其注入Controller的构造函数,而不是使用默认实现的GetOwinContext功能.
为了做到这一点,我在Startup的ConfigureAuth(IAppBuilder app)方法中注释掉了以下行,默认情况下会创建UserManager类:
// app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
Run Code Online (Sandbox Code Playgroud)
相反,我使用了在安装Ninject.Web.Common.Webhost nuget包时创建的NinjectWebCommon来创建相关的绑定.
这个实现在一些UserManager操作中运行良好,但是对于一些操作,例如ResetPasswordAsync,它失败了,因为没有调用默认的ApplicationUserManager实现,因此从未设置UserManager类中的UserTokenProvider:
    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
    {
        var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
        // Configure validation logic for usernames
        manager.UserValidator = new UserValidator<ApplicationUser>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };
        // Configure validation logic for passwords
        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };
        // Register two factor authentication providers. …Run Code Online (Sandbox Code Playgroud) 我一直在研究新版ASP.NET Identity 2.1的新功能,其中一项增强功能是集成到OWIN中间件中的新IoC功能.我在这些例子中看到的一句话是这一句:
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
Run Code Online (Sandbox Code Playgroud)
这句话接收一个函数委托,它返回示例中提供的管理器实现的新实例:
 public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,
        IOwinContext context)
    {
        var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>())); 
Run Code Online (Sandbox Code Playgroud)
我个人不喜欢这个实现,因为我无法使用容器为这些管理器注入任何我想要的依赖项.
还有一个"神奇地""IdentityFactoryOptions"和"IOwinContext"被"神奇地"注入到我无法进入我的IoC容器的函数中.
有没有人对此实施有更好的解决方法?
我研究外部登录策略,术语让我困惑.以下是什么关系.
我是OWIN身份验证的新手,我必须误解一切是如何工作的,但我无法在任何地方找到这个.
我想要的是能够使用中央域进行身份验证.如果有人apps.domain.com在未经过身份验证时尝试访问,则会将其重定向到accounts.domain.com/login以便将所有身份验证分成其自己的域和应用程序.使用MVC 4表单身份验证非常容易,您可以在其中指定完整的URL,但似乎不适用于OWIN.
在Startup.Auth.cs:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    LoginPath = new PathString("/account/login")
}
Run Code Online (Sandbox Code Playgroud)
使用该CookieDomain选项设置cookie时,可以轻松指定域.但是,当您指定要重定向到的登录路径时,它必须相对于当前应用程序,那么如何在MVC 4表单身份验证中完成哪些操作非常简单?
在没有深入了解OWIN身份验证的全部内容的情况下,经过几个小时的搜索后,我找不到任何解决此问题的方法.