将OAuth2添加到现有的MVC5项目

时间:2019-03-08 17:18:22

标签: oauth-2.0 asp.net-mvc-5 owin

我有一个现有的MVC 5项目,当前使用Owin身份验证,并且我只需要将OAuth2添加到一个新的MVC控制器中。对于新控制器,我仅需要OAuth2身份验证,而不需要当前身份验证。对于其余的控制器,我需要身份验证保持不变。我知道如何设置OAuth2,但我需要弄清楚如何将两者分开,以便分别使用。下面是Startup.Authorization.cs

程序集:OwinStartup(typeof(Startup),“ ConfigureAuth”)]

命名空间XXXX {     公共类创业     {

    public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();
        ConfigureAuth(app);
        WebApiConfig.Register(config);
        app.UseCors(CorsOptions.AllowAll);

    }

public void ConfigureAuth(IAppBuilder应用)         {

            app.CreatePerOwinContext(CreateIdentityUserContext);
            app.CreatePerOwinContext<IdentityUserManager>(IdentityUserManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                ExpireTimeSpan = TimeSpan.FromMinutes(this.TimeoutMinutes),
                SlidingExpiration = true,
                LoginPath = new PathString(this.LoginUrl),
                Provider = new CookieAuthenticationProvider
                {           
            OnApplyRedirect = ctx =>
                    {
                        if (!IsAjaxRequest(ctx.Request))
                        {
                            ctx.Response.Redirect(ctx.RedirectUri);
                        }
                    }
                }
            });
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);


            app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));


        app.UseCookieAuthentication(new CookieAuthenticationOptions()
        {

                CookieName = ".AspNet." + DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie,
                AuthenticationType = DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie,
                AuthenticationMode = AuthenticationMode.Passive,
                ExpireTimeSpan = TimeSpan.FromDays(90),
                SlidingExpiration = false,
                Provider = new CookieAuthenticationProvider
                {
                    OnResponseSignIn = ctx =>
                    {
                        ctx.OwinContext.Set("auth-prop-expires", ctx.Properties.ExpiresUtc);
                        ctx.OwinContext.Set("auth-prop-persist", ctx.Properties.IsPersistent);
                        var issued = ctx.Properties.IssuedUtc ?? DateTimeOffset.UtcNow;
                        ctx.Properties.IsPersistent = true;
                    },
                    OnResponseSignedIn = ctx =>
                    {
                        ctx.Properties.ExpiresUtc = ctx.OwinContext.Get<DateTimeOffset?>("auth-prop-expires");
                        ctx.Properties.IsPersistent = ctx.OwinContext.Get<bool>("auth-prop-persist");
                    }
                }
            });     

          OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
          {
              AllowInsecureHttp = true,
              TokenEndpointPath = new PathString("/token"),
              AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
              Provider = new Providers.AuthorizationServerProvider()
          };

          // Token Generation
          app.UseOAuthAuthorizationServer(OAuthServerOptions);
          app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

0 个答案:

没有答案
相关问题