public static IdentityUser GetCurrentIdentityUser() { if (!OwinHelper.GetOwinContext(HttpContext.Current).Request.User.Identity.IsAuthenticated) { return(null); } var manager = GetUserManager(); return(manager.FindById(OwinHelper.GetOwinContext(HttpContext.Current).Request.User.Identity.Name)); }
public static IEnumerable <string> CreateAndLoginUser(string username, string email, string name, string userClass = null) { var loginInfo = OwinHelper.GetOwinContext(HttpContext.Current).Authentication.GetExternalLoginInfo(); if (loginInfo == null) { //RedirectOnFail(HttpContext.Current.Response); return(new[] { "No External Login information found." }); } return(CreateAndLoginUser(username, email, name, loginInfo.ExternalIdentity.Claims, loginInfo, userClass)); }
public static bool IsAuthenticatedInMode(Type authType) { var identity = OwinHelper.GetOwinContext(HttpContext.Current).Request?.User?.Identity; return ( ( (identity != null) && (identity.IsAuthenticated) ) && ( (authType == null) || (identity.GetType() == authType) ) ); }
public static bool CheckAccess(string resourceType, string resource, string action, string userName) { var resourceAction = new ResourceAction(resourceType, resource, ClaimTypes.ActionType, action); var user = IdentityHelper.GetIdentityUserByName(userName); var manager = IdentityHelper.GetUserManager(); ClaimsIdentity userIdentity = null; if (user == null) { log4net.LogManager.GetLogger(nameof(ClaimPermission)).Warn($"A user with username '{userName}' was not found by user manager. Creating Claims Identity from Owin Context Request User.Identity ..."); userIdentity = (OwinHelper.GetOwinContext(HttpContext.Current).Request.User.Identity as ClaimsIdentity); } else { userIdentity = user.GenerateUserIdentityAsync(manager).Result; } var context = CreateAuthorizationContext(ClaimsPrincipal.Current, resourceAction); var claimsAuthorizationManager = FederatedAuthentication.FederationConfiguration.IdentityConfiguration.ClaimsAuthorizationManager as AuthorizationManager; return(claimsAuthorizationManager.CheckAccess(context, userIdentity)); }
public static bool ValidateExternalLoginAndLogin(out ApplicationUser appUser, out IEnumerable <string> errors) { errors = new List <string>(); appUser = null; // Get external login info var manager = GetUserManager(); var loginInfo = OwinHelper.GetOwinContext(HttpContext.Current).Authentication.GetExternalLoginInfo(); if (loginInfo == null) { //RedirectOnFail(HttpContext.Current.Response); errors = new[] { "No External Login information found." }; return(false); } // Find local user associated with the external login var user = manager.Find(loginInfo.Login); if (user != null) { // If local user exists -> Login SignIn(loginInfo, isPersistent: false); appUser = user.User; return(true); //IdentityHelper.RedirectToReturnUrl(HttpContext.Current.Request.QueryString["ReturnUrl"], HttpContext.Current.Response); } if (HttpContext.Current.User.Identity.IsAuthenticated) { // If the external login is not associated with the currently logged in User -> associate // Apply Xsrf check when linking loginInfo = OwinHelper.GetOwinContext(HttpContext.Current).Authentication.GetExternalLoginInfo(XsrfKey, HttpContext.Current.User.Identity.GetUserId()); if (loginInfo == null) { errors = new[] { "No External Login information found." }; return(false); } var id = HttpContext.Current.User.Identity.GetUserId(); var addLoginResult = manager.AddLogin(id, loginInfo.Login); if (addLoginResult.Succeeded) { // If successfully associated the user with the external login -> redirect to ReturnUrl //IdentityHelper.RedirectToReturnUrl(HttpContext.Current.Request.QueryString["ReturnUrl"], HttpContext.Current.Response); appUser = manager.Find(loginInfo.Login).User; return(true); } // Failure to associate -> Return any Errors (result is already populated) errors = addLoginResult.Errors; return(false); } // If not local user exists and no user is already logged in in this session -> return to UI to get more info errors = new[] { "No local user exists. Please create your local user by providing the following details." }; appUser = new ApplicationUser { Email = loginInfo.Email, UserName = loginInfo.DefaultUserName, Name = loginInfo.DefaultUserName, Claims = new List <ApplicationUserClaim>() }; foreach (var claim in loginInfo.ExternalIdentity.Claims) { appUser.AddClaims(new ApplicationUserClaim { ClaimType = claim.Type, ClaimValue = claim.Value, ClaimValueType = claim.ValueType, Issuer = claim.Issuer, OriginalIssuer = claim.OriginalIssuer, }); } return(false); }
public static SignInManager <IdentityUser, string> GetSignInManager() { return(OwinHelper.GetOwinContext(HttpContext.Current)?.Get <SignInManager <IdentityUser, string> >()); }
public static string GetCurrentUserName() { return(OwinHelper.GetOwinContext(HttpContext.Current).Request.User.Identity.Name); }