public async Task <ActionResult> Index(string id, string code) { var ctx = Request.GetOwinContext(); var user = await ctx.Environment.GetIdentityServerPartialLoginAsync(); if (user == null) { return(View("Error")); } var subjectid = user.FindFirst("sub").Value; var context = Request.GetOwinContext(); var env = Request.GetOwinContext().Environment; var signInMessage = env.GetSignInMessage(id); OwinEnvironmentService owin = new OwinEnvironmentService(env); MidasUserService userService = new MidasUserService(owin); if (!(await userService.VerifyTwoFactorTokenAsync(subjectid, code, signInMessage))) { ViewData["message"] = "Incorrect code"; return(View("Index")); } var claims = user.Claims.Where(c => c.Type != "amr").ToList(); claims.Add(new Claim("amr", "2fa")); await ctx.Environment.UpdatePartialLoginClaimsAsync(claims); var resumeUrl = await ctx.Environment.GetPartialLoginResumeUrlAsync(); return(Redirect(resumeUrl)); }
public override Task AuthenticateLocalAsync(LocalAuthenticationContext context) { //var user = Users.SingleOrDefault(x => x.Username == context.UserName && x.Password == context.Password); MidasUserService userService = new MidasUserService(); var user = userService.GetUser(context.UserName, context.Password); if (user.Subject != null) { context.AuthenticateResult = new AuthenticateResult(user.Subject, user.Username); } return(Task.FromResult(0)); }
public override Task GetProfileDataAsync(ProfileDataRequestContext context) { // issue the claims for the user //var user = Users.SingleOrDefault(x => x.Subject == context.Subject.GetSubjectId()); MidasUserService userService = new MidasUserService(); var user = userService.GetUserProfileData(Convert.ToInt32(context.Subject.GetSubjectId())); user.Claims = GetUserClaims(user); if (user != null) { context.IssuedClaims = user.Claims.Where(x => context.RequestedClaimTypes.Contains(x.Type)); } return(Task.FromResult(0)); }
public async Task <ActionResult> Index(string id, LoginCredential model) { var context = Request.GetOwinContext(); var env = Request.GetOwinContext().Environment; var signInMessage = env.GetSignInMessage(id); var authenticationContext = new LocalAuthenticationContext { UserName = model.Username.Trim(), Password = model.Password.Trim(), SignInMessage = signInMessage }; OwinEnvironmentService owin = new OwinEnvironmentService(env); MidasUserService userService = new MidasUserService(owin); await userService.AuthenticateLocalAsync(authenticationContext); var authResult = authenticationContext.AuthenticateResult; if (authResult == null || (authResult.ErrorMessage != null && authResult.ErrorMessage != string.Empty)) { string errorMessage = null; if (authResult != null && authResult.ErrorMessage != string.Empty) { errorMessage = authResult.ErrorMessage; } else { errorMessage = "Unable to process you authentication request due an error"; } ModelState.AddModelError("AuthError", errorMessage); return(View()); } else { ClearAuthenticationCookiesForNewSignIn(context, authResult); IssueAuthenticationCookie(context, id, authResult, model.RememberMe); //var redirectUrl = GetRedirectUrl(context, signInMessage, authResult); string redirectUrl; if (authResult.IsPartialSignIn) { var path = authResult.PartialSignInRedirectPath; if (path.StartsWith("~/")) { path = path.Substring(2); path = context.Environment.GetIdentityServerBaseUrl() + path; } var host = new Uri(context.Environment.GetIdentityServerHost()); //return new Uri(host, path); redirectUrl = path; } else { redirectUrl = signInMessage.ReturnUrl; } return(Redirect(redirectUrl)); } }