I have 2 web applications that share the same core level domain as below so that I can share cookies. Web.conifg in both projects has the same machine key and verification key. Since I want to use identifiers and NOT authenticaiton forms, I do not have a node file in any of my web.config files. I can successfully create an Auth cookie from SSO and view the browsing pages in SSO, but I'm still redirected to logging in to SSO when I try to access an authorized view in an MVC project.
- sso.domain.com - MVC Project
- mvc.domain.com - MVC Project
I have a startup.cs file in my SSO and MVC project as shown below:
public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); }
Below is the code that I still have in the SSO project under AccountController.cs. I call the IdentitySignin function below when checking the user against the database that creates the cookie:
private void IdentitySignin(string userId, string name, string providerKey = null, bool isPersistent = false) { var claims = new List<Claim>(); // create *required* claims claims.Add(new Claim(ClaimTypes.NameIdentifier, userId)); claims.Add(new Claim(ClaimTypes.Name, name)); var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); //get the expiry minutes from config or use the default value of 30 minutes double expiryMinutes; expiryMinutes = double.TryParse(ConfigurationManager.AppSettings["AuthCookieExpiryMinutes"], out expiryMinutes) ? expiryMinutes : 30; // add to user here! AuthenticationManager.SignIn(new AuthenticationProperties() { AllowRefresh = true, IsPersistent = isPersistent, ExpiresUtc = DateTime.UtcNow.AddMinutes(expiryMinutes), IssuedUtc = DateTime.UtcNow }, identity); } private void IdentitySignout() { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalCookie); } private IAuthenticationManager AuthenticationManager { get { return HttpContext.GetOwinContext().Authentication; } } private async Task<string> GetVerifiedUserIdAsync() { var result = await AuthenticationManager.AuthenticateAsync( DefaultAuthenticationTypes.ApplicationCookie); if (result != null && result.Identity != null && !String.IsNullOrEmpty(result.Identity.GetUserId())) { return result.Identity.GetUserId(); } return null; }
source share