Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC OWIN and SignalR - two Startup.cs files

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 = ""
            //});
        }
    }
}
like image 888
exe.bat Avatar asked Jul 17 '15 15:07

exe.bat


People also ask

How to Add OWIN Startup class in MVC?

In Visual Studio 2017 right-click the project and select Add Class. - In the Add New Item dialog box, enter OWIN in the search field, and change the name to Startup. cs, and then select Add. The next time you want to add an Owin Startup class, it will be in available from the Add menu.

How to Add Startup CS file?

Now To add Startup. cs to ASP.NET Core 6.0 project, add a new class named Startup. cs and add the below code. So in the above code, you can see, that we have added a startup class and added 2 methods ConfigureServices and Configure as we used to have in the previous version of asp.net.

What is Signalr MVC?

Description. The SignalR permits bidirectional communication between the server and client. Its availability helps the server to push the content to connected clients instantly.


1 Answers

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.

like image 84
danludwig Avatar answered Oct 02 '22 00:10

danludwig