使用OAUTH 2的ASP.NET MVC外部自定义登录提供程序

时间:2015-09-11 15:16:36

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

我在使用ASP.NET MVC 5的新项目中工作,我们希望使用自定义身份服务器对所有用户进行身份验证。我们使用OWIN和OAuth 2.0代码授权流程。现在我们遇到了身份验证问题,我们第一次尝试验证时可以做到,但第二次我们不能。再次进行身份验证的唯一方法是在iis中回收应用程序池。

这就是我们配置身份验证中间件的方法

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
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            ExpireTimeSpan = System.TimeSpan.FromMinutes(cookieExpirationTime),
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });

        // Use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        app.UseCustomAuthentication(
            "Client_id",
            "Client_secret"
            );
    }

在我们的AccountController中,方法 ExternalLoginCallback 的第一行始终返回null,在回收应用程序池后,它返回一个loginInfo,我们可以登录。

public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        if (loginInfo == null)
        {
            return RedirectToAction("Login");
        }

        // Sign in the user with this external login provider if the user already has a login
        var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);
        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return RedirectToAction("Login");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("Login");
            case SignInStatus.Failure:
            default:
                 ViewBag.ReturnUrl = returnUrl;
                ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { UserName = loginInfo.DefaultUserName });
        }
    }

我们使用默认的visual studio Web应用程序模板测试了我们的自定义中间件,并且它有效。谁知道发生了什么?

注意:我们使用Ninject作为DI容器。

0 个答案:

没有答案
相关问题