public async Task <IHttpActionResult> AddExternalLogin(AddExternalLoginBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); var ticket = AccessTokenFormat.Unprotect(model.ExternalAccessToken); if (ticket?.Identity == null || (ticket.Properties?.ExpiresUtc != null && ticket.Properties.ExpiresUtc.Value < DateTimeOffset.UtcNow)) { return(BadRequest("External login failure.")); } var externalData = ExternalLoginData.FromIdentity(ticket.Identity); if (externalData == null) { return(BadRequest("The external login is already associated with an account.")); } var result = await UserManager.AddLoginAsync(IdentityExtensions.GetUserId(User.Identity), new UserLoginInfo(externalData.LoginProvider, externalData.ProviderKey)); if (!result.Succeeded) { return(GetErrorResult(result)); } return(Ok()); }
public async Task <IHttpActionResult> RemoveLogin(RemoveLoginBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } IdentityResult result; if (model.LoginProvider == LocalLoginProvider) { result = await UserManager.RemovePasswordAsync(IdentityExtensions.GetUserId(User.Identity)); } else { result = await UserManager.RemoveLoginAsync(IdentityExtensions.GetUserId(User.Identity), new UserLoginInfo(model.LoginProvider, model.ProviderKey)); } if (!result.Succeeded) { return(GetErrorResult(result)); } return(Ok()); }
public async Task <IHttpActionResult> SetPassword(SetPasswordBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = await UserManager.AddPasswordAsync(IdentityExtensions.GetUserId(User.Identity), model.NewPassword); if (!result.Succeeded) { return(GetErrorResult(result)); } return(Ok()); }
public async Task <ManageInfoViewModel> GetManageInfo(string returnUrl, bool generateState = false) { User user = await UserManager.FindByIdAsync(IdentityExtensions.GetUserId(User.Identity)); if (user == null) { return(null); } var logins = new List <UserLoginInfoViewModel>(); foreach (var linkedAccount in user.Logins) { logins.Add(new UserLoginInfoViewModel { LoginProvider = linkedAccount.LoginProvider, ProviderKey = linkedAccount.ProviderKey }); } if (user.PasswordHash != null) { logins.Add(new UserLoginInfoViewModel { LoginProvider = LocalLoginProvider, ProviderKey = user.UserName, }); } return(new ManageInfoViewModel { LocalLoginProvider = LocalLoginProvider, Email = user.UserName, Logins = logins, ExternalLoginProviders = GetExternalLogins(returnUrl, generateState) }); }