ASP.NET MVC OWIN和SignalR - 两个Startup.cs文件 [英] ASP.NET MVC OWIN and SignalR - two Startup.cs files

查看:5689
本文介绍了ASP.NET MVC OWIN和SignalR - 两个Startup.cs文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我的项目。

我使用ASP.NET MVC与ASP.NET 2.0身份验证和我说SignalR到项目所以现在我有两个Startup.cs文件:

从MVC首先一个在根

  [汇编:OwinStartupAttribute(typeof运算(MCWeb_3SR.Startup))]
命名空间MCWeb_3SR
{
    公共部分类启动
    {
        公共无效配置(IAppBuilder应用程序)
        {
            ConfigureAuth(应用);        }
    }
}

和SignalR之一,应用code文件夹

  [汇编:OwinStartup(typeof运算(SignalRChat.Startup))]
命名空间SignalRChat
{
    公共类启动
    {
        公共无效配置(IAppBuilder应用程序)
        {            VAR心跳= GlobalHost.DependencyResolver.Resolve< ITransportHeartbeat>();            VAR显示器=新的presenceMonitor(心跳);
            monitor.StartMonitoring();
            //任何连接或集线器线和配置应该在这里
            app.MapSignalR();
        }
    }
}

但我得到了以下错误

尝试加载应用程序时出现以下错误。
- 该OwinStartup属性汇编发现MCWeb-3SR引用启动类型MCWeb_3SR.Startup在程序集APP_ code.hszoxs_z属性冲突引用启动类型SignalRChat.ChatStartup,因为它们具有相同的FriendlyName 。删除或重命名的属性之一,或直接引用所需的类型。
在你的web.config一的假值AutomaticAppStartup:要禁用OWIN启动发现,添加appSetting owin。
在你的web.config使用完全限定的启动类或配置的方法名AppStartup

:要指定OWIN启动大会,类或方法,添加appSetting owin。

我尝试添加

  [汇编:OwinStartupAttribute(ProductionConfiguration.st的typeof(MCWeb_3SR.Startup))]

到第一位,页运行,但不会验证工作。

我怎么能有两个人一起跑?

更新

 使用系统;
使用Microsoft.AspNet.Identity;
使用Microsoft.AspNet.Identity.Owin;
使用Microsoft.Owin;
使用Microsoft.Owin.Security.Cookies;
使用Microsoft.Owin.Security.Google;
使用Owin;
使用MCWeb_3SR.Models;命名空间MCWeb_3SR
{
    公共部分类启动
    {
        //欲了解更多有关配置验证,请访问http://go.microsoft.com/fwlink/?LinkId=301864
        公共无效ConfigureAuth(IAppBuilder应用程序)
        {
            //配置分贝范围内,用户管理和登入管理器使用每个请求的单个实例
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext< ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext< ApplicationSignInManager>(ApplicationSignInManager.Create);            //使应用程序能够使用cookie来存储信息,在用户签订
            //并利用co​​okie来临时存储有关用户记录的信息与第三方供应商登录
            //配置在cookie中的标志
            app.UseCookieAuthentication(新CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LOGINPATH =新PathString(/帐号/登录),
                供应商=新CookieAuthenticationProvider
                {
                    //允许应用验证安全戳当用户登录英寸
                    //这是一项安全功能,当您更改密码或添加外部登录到您的帐户被使用。
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity< ApplicationUserManager,ApplicationUser>(
                        validateInterval:TimeSpan.FromMinutes(30),
                        regenerateIdentity:(经理,用户)=> user.GenerateUserIdentityAsync(经理))
                }
            });
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);            //使应用程序能够临时存储时,他们正在核实在双因素身份验证过程的第二个因素用户信息。
            app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie,TimeSpan.FromMinutes(5));            //使应用程序能够记住第二次登录验证的因素,如电话或电子邮件。
            //一旦选中此选项,在登录过程中验证的第二个步骤将是你从登录的设备记住。
            //这是类似的,当你登录了rememberMe选项。
            app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);            //取消注释以下行来实现与第三方供应商登录登录
            //app.UseMicrosoftAccountAuthentication(
            //的clientId:,
            // clientSecret:);            //app.UseTwitterAuthentication(
            // consumerKey:,
            // consumerSecret:);            //app.UseFacebookAuthentication(
            // APPID:,
            // appSecret:);            //app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
            // {
            //客户端Id =,
            // ClientSecret =
            //});
        }
    }
}


解决方案

将您的signalr启动文件到App_Start文件夹并将其重命名为Startup.SignalR.cs。它应该有这样的内容,注意配置方法已重命名为ConfigureSignalR:

 命名空间MCWeb_3SR
{
  公共部分类启动
  {
    公共无效ConfigureSignalR(IAppBuilder应用程序){
      VAR心跳= GlobalHost.DependencyResolver.Resolve< ITransportHeartbeat>();      VAR显示器=新的presenceMonitor(心跳);
      monitor.StartMonitoring();
      //任何连接或集线器线和配置应该在这里
      app.MapSignalR();
    }
  }
}

现在在Startup.cs文件在你项目的根,加ConfigureSignalR(应用程序)调用右后ConfigureAuth(APP):

  [汇编:OwinStartup(typeof运算(MCWeb_3SR.Startup))]
命名空间MCWeb_3SR
{
  公共部分类启动
  {
    公共无效配置(IAppBuilder应用程序){
      ConfigureAuth(应用);
      ConfigureSignalR(应用);
    }
  }
}

只要所有启动部分类具有相同的命名空间,这应该工作。

I have a problem with my project.

I use ASP.NET MVC with ASP.NET Identity 2.0 for authentication and I added SignalR to the project so now I have two Startup.cs files:

First one from MVC in the root

[assembly: OwinStartupAttribute(typeof(MCWeb_3SR.Startup))]
namespace MCWeb_3SR
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);

        }
    }
}

And SignalR one in AppCode folder

[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {

            var heartBeat = GlobalHost.DependencyResolver.Resolve<ITransportHeartbeat>();

            var monitor = new PresenceMonitor(heartBeat);
            monitor.StartMonitoring();


            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }
    }
}

but I get the following error

The following errors occurred while attempting to load the app. - The OwinStartup attribute discovered in assembly 'MCWeb-3SR' referencing startup type 'MCWeb_3SR.Startup' conflicts with the attribute in assembly 'App_Code.hszoxs_z' referencing startup type 'SignalRChat.ChatStartup' because they have the same FriendlyName ''. Remove or rename one of the attributes, or reference the desired type directly. To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config. To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.

I tried adding

[assembly: OwinStartupAttribute("ProductionConfiguration.st", typeof(MCWeb_3SR.Startup))]

to the first one, page runs but authentication wont work.

How can I have both of them run together ?

Update

using System;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.Google;
using Owin;
using MCWeb_3SR.Models;

namespace MCWeb_3SR
{
    public partial class Startup
    {
        // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
        public void ConfigureAuth(IAppBuilder app)
        {
            // Configure the db context, user manager and signin manager to use a single instance per request
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

            // Enable the application to use a cookie to store information for the signed in user
            // and to use a cookie to temporarily store information about a user logging in with a third party login provider
            // Configure the sign in cookie
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
                    // Enables the application to validate the security stamp when the user logs in.
                    // This is a security feature which is used when you change a password or add an external login to your account.  
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                }
            });            
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            // Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
            app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));

            // Enables the application to remember the second login verification factor such as phone or email.
            // Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
            // This is similar to the RememberMe option when you log in.
            app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);

            // Uncomment the following lines to enable logging in with third party login providers
            //app.UseMicrosoftAccountAuthentication(
            //    clientId: "",
            //    clientSecret: "");

            //app.UseTwitterAuthentication(
            //   consumerKey: "",
            //   consumerSecret: "");

            //app.UseFacebookAuthentication(
            //   appId: "",
            //   appSecret: "");

            //app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
            //{
            //    ClientId = "",
            //    ClientSecret = ""
            //});
        }
    }
}

解决方案

Move your signalr startup file to the App_Start folder and rename it to Startup.SignalR.cs. It should have this content, note that the Configure method has been renamed to ConfigureSignalR:

namespace MCWeb_3SR
{
  public partial class Startup
  {
    public void ConfigureSignalR(IAppBuilder app) {
      var heartBeat = GlobalHost.DependencyResolver.Resolve<ITransportHeartbeat>(); 

      var monitor = new PresenceMonitor(heartBeat); 
      monitor.StartMonitoring(); 


      // Any connection or hub wire up and configuration should go here 
      app.MapSignalR();
    }
  }
}

Now in the Startup.cs file at the root of your project, add a ConfigureSignalR(app) call right after ConfigureAuth(app):

[assembly: OwinStartup(typeof(MCWeb_3SR.Startup))]
namespace MCWeb_3SR
{
  public partial class Startup
  {
    public void Configuration(IAppBuilder app) {
      ConfigureAuth(app);
      ConfigureSignalR(app);
    }
  }
}

As long as all of the Startup partial classes have the same namespace, this should work.

这篇关于ASP.NET MVC OWIN和SignalR - 两个Startup.cs文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆