Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSO not working! #100

Open
anshulMetacube opened this issue May 31, 2023 · 0 comments
Open

SSO not working! #100

anshulMetacube opened this issue May 31, 2023 · 0 comments

Comments

@anshulMetacube
Copy link

anshulMetacube commented May 31, 2023

I made an app .NET Framework 4.8 , guiding myself by looking at your way of implementation.
(I may have changed some line here and there to try different things but couldn't succeed)

AccountController :

public class AccountController : Controller
	{
		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult Login(FormCollection form)
		{
			if (!HttpContext.User.Identity.IsAuthenticated)
			{
				var properties = new AuthenticationProperties();
				properties.Dictionary.Add("sessionToken", form.Get("sessionToken"));
				properties.RedirectUri = "/Home/About";

				HttpContext.GetOwinContext().Authentication.Challenge(properties,
					OktaDefaults.MvcAuthenticationType);

				return new HttpUnauthorizedResult();
			}

			return RedirectToAction("Index", "Home");
		}

		public ActionResult Login()
		{
			return View();
		}

		[HttpPost]
		public ActionResult Logout()
		{
			if (HttpContext.User.Identity.IsAuthenticated)
			{
				HttpContext.GetOwinContext().Authentication.SignOut(
					CookieAuthenticationDefaults.AuthenticationType,
					OktaDefaults.MvcAuthenticationType);
			}

			return RedirectToAction("Index", "Home");
		}

		public ActionResult PostLogout()
		{
			return RedirectToAction("Index", "Home");
		}
	}

Login View:

@using System.Configuration;
@{
    ViewData["Title"] = "Login";
}

<script src="https://global.oktacdn.com/okta-signin-widget/7.6.1/js/okta-sign-in.min.js" type="text/javascript"></script>
<link href="https://global.oktacdn.com/okta-signin-widget/7.6.1/css/okta-sign-in.min.css" type="text/css" rel="stylesheet" />

<div id="widget"></div>

<form method="POST" asp-action="Login">
    <input type="hidden" name="sessionToken" id="hiddenSessionTokenField" />
    @Html.AntiForgeryToken()
</form>

<script type="text/javascript">
    const signIn = new OktaSignIn({
        baseUrl: '@ConfigurationManager.AppSettings["okta:OktaDomain"]'
    });

    signIn.renderEl({ el: '#widget' }, (res) => {
        var sessionTokenField = $("#hiddenSessionTokenField");
        sessionTokenField.val(res.session.token);
        var form = sessionTokenField.parent();
        form.submit();
    }, (err) => {
        console.error(err);
    });
</script>

Expected Behaviour :

Whenever I sign in to okta dev console app in an tab, my app on other tab automatically signs in on reload or when clicked on login button and vice versa (i.e. if I sign in to my app, Okta dev console automatically sign in).

Actual Behaviour :

The app works fine when an instance of the same app is signed in, the other instance of the same app automatically signs in on reload. But, it doesn't work when I sign in to Okta, my app on other tab doesn't signs in!

Note : When I sign in to my app, Okta gets signed in (that is they have implemented such feature that recognises that another app is signed in but the domain is common.)

@bryanapellanes-okta
Official Sample that I followed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant