8
votes

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 = ""
            //});
        }
    }
}
2
I need both classes - one is for authentication and other is for SignalRexe.bat
Can you not put all the configuration content into one method and use 2 attributes? e.g. [assembly: OwinStartupAttribute(typeof(MCWeb_3SR.Startup))] [assembly: OwinStartup(typeof(MCWeb_3SR.Startup))]HockeyJ
I tried that before but if I cant access App_Code folder from root Startup.cs and I cant get ConfigureAuth(app) from the other one.exe.bat

2 Answers

10
votes

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.

0
votes

Could you put both sets of code into one single OwinStartup file?

namespace MCWeb_3SR
{
    public class OwinStartup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(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();
        }
    }
}