noelbundick / Owin.Security.CAS

OWIN middleware to support Jasig CAS
MIT License
8 stars 5 forks source link

Redirect page after login #3

Open mspace23 opened 8 years ago

mspace23 commented 8 years ago

Hello! How can I set the page that will redirect after the user has login in MVC 5 app?. Thank you

sergiomcalzada commented 8 years ago

Setting the config property CallbackPath?

mspace23 commented 8 years ago

I try

casOptions.CallbackPath = new PathString("/Home/Index");

but nothing. It always returns me to the the page that I have log in. (is that the default setting?) What I am doing wrong? (I am working on localhost)

mspace23 commented 8 years ago

i dont know if this correct but I change the RedirectToAction in ExternalLoginCallback at the Account Controller to "Index" "Home" and it worked

mspace23 commented 8 years ago

My approach was not correct. The logInfo always returns to null so I have no success login. Do you know how can I allow to user to login without to register him to database? I try with cookie but no success. Any Idea? Thank you

My start up

        ` app.UseCookieAuthentication(new CookieAuthenticationOptions
           {
               //CookieManager = new SystemWebCookieManager(),
               AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
               AuthenticationMode = AuthenticationMode.Active,

               LoginPath = new PathString("/Account/Login"),
              Provider = new CookieAuthenticationProvider
          {
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager,   ApplicationUser>(
                validateInterval: TimeSpan.FromMinutes(30),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
        }
    });

        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
        CasAuthenticationOptions casOptions = new CasAuthenticationOptions();
        casOptions.CasServerUrlBase = "myprovider";
        casOptions.Caption = "Single Sign-On";
        casOptions.AuthenticationType = "SO";
       // casOptions.CallbackPath = new PathString("/Index");
        app.UseCasAuthentication(casOptions);`

My  Controller
     ` public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
      {

        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

        if (loginInfo == null)
        {
            //return RedirectToAction("Index", "Home");
           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 View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
            case SignInStatus.Failure:
            default:

                // Added this section to automatically create account if we have an email address
                if (!string.IsNullOrEmpty(loginInfo.Email))
                {
                    var user = new ApplicationUser { UserName = loginInfo.Email, Email = loginInfo.Email };
                    var createUserResult = await UserManager.CreateAsync(user);
                    if (createUserResult.Succeeded)
                    {
                        var addLoginResult = await UserManager.AddLoginAsync(user.Id, loginInfo.Login);
                        if (addLoginResult.Succeeded)
                        {
                            await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                            return RedirectToLocal(returnUrl);
                        }
                    }
                }

                // If the user does not have an account, then prompt the user to create an account
                ViewBag.ReturnUrl = returnUrl;
                ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email });
        }

     }`
bdm4 commented 8 years ago

@mspace23 I think there is an issue inside the validator code. Where the claim identity issued has the wrong AuthenticationType set. See issue #5. I have a sample project that's pretty close to this as well and this is the only difference I can see. This also allows me to use the [Authorize] attribute on controllers and actions.