public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var userManager = context.OwinContext.GetUserManager <ApplicationUserManager>(); //RAIdentityUser user = userManager.Find(context.UserName, context.Password); RAIdentityUser user = await userManager.FindAsync(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, OAuthDefaults.AuthenticationType); ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager, CookieAuthenticationDefaults.AuthenticationType); AuthenticationProperties properties = CreateProperties(user.UserName); AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); context.Validated(ticket); context.Request.Context.Authentication.SignIn(cookiesIdentity); }
public async Task <IHttpActionResult> GetExternalLogin(string provider, string error = null) { if (error != null) { return(Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error))); } if (!User.Identity.IsAuthenticated) { return(new ChallengeResult(provider, this)); } ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity); if (externalLogin == null) { return(InternalServerError()); } if (externalLogin.LoginProvider != provider) { Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); return(new ChallengeResult(provider, this)); } RAIdentityUser user = await UserManager.FindAsync(new UserLoginInfo(externalLogin.LoginProvider, externalLogin.ProviderKey)); bool hasRegistered = user != null; if (hasRegistered) { Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager, OAuthDefaults.AuthenticationType); ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(UserManager, CookieAuthenticationDefaults.AuthenticationType); AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user.UserName); Authentication.SignIn(properties, oAuthIdentity, cookieIdentity); } else { IEnumerable <Claim> claims = externalLogin.GetClaims(); ClaimsIdentity identity = new ClaimsIdentity(claims, OAuthDefaults.AuthenticationType); Authentication.SignIn(identity); } return(Ok()); }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { //var allowedOrigin = "*"; //context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin }); ApplicationUserManager userManager = context.OwinContext.GetUserManager <ApplicationUserManager>(); RAIdentityUser user = await userManager.FindAsync(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect.!!!!"); return; } //if (!user.EmailConfirmed) //{ // context.SetError("invalid_grant", "AppUser did not confirm email."); // return; //} ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT"); var ticket = new AuthenticationTicket(oAuthIdentity, null); context.Validated(ticket); }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var allowedOrigin = "*"; context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin }); ApplicationUserManager userManager = context.OwinContext.GetUserManager <ApplicationUserManager>(); RAIdentityUser user = await userManager.FindAsync(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect.!!!!"); return; } RADBContext db = new RADBContext(); string fullName = db.AppUsers.SingleOrDefault(r => r.UserId == user.AppUserId).FullName; ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT"); oAuthIdentity.AddClaim(new Claim("UserFullName", fullName)); var ticket = new AuthenticationTicket(oAuthIdentity, null); context.Validated(ticket); }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var allowedOrigin = "*"; context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin }); //context.OwinContext.Response.Headers.Add("Access-Control-Allow-Methods", new[] { "POST", "GET", "PUT", "DELETE", "OPTIONS" }); ApplicationUserManager userManager = context.OwinContext.GetUserManager <ApplicationUserManager>(); RAIdentityUser user = await userManager.FindAsync(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect.!!!!"); return; } context.OwinContext.Response.Headers.Add("Email", new[] { user.Email }); //if (!user.EmailConfirmed) //{ // context.SetError("invalid_grant", "AppUser did not confirm email."); // return; //} if (await userManager.IsInRoleAsync(user.UserName, "Admin")) { context.OwinContext.Response.Headers.Add("Role", new[] { "Admin" }); } else { if (await userManager.IsInRoleAsync(user.UserName, "Manager")) { context.OwinContext.Response.Headers.Add("Role", new[] { "Manager" }); } else { context.OwinContext.Response.Headers.Add("Role", new[] { "AppUser" }); } } context.OwinContext.Response.Headers.Add("Access-Control-Expose-Headers", new[] { "Role", "Email" }); ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT"); var ticket = new AuthenticationTicket(oAuthIdentity, null); context.Validated(ticket); }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var allowedOrigin = "*"; context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin }); ApplicationUserManager userManager = context.OwinContext.GetUserManager <ApplicationUserManager>(); RAIdentityUser user = await userManager.FindAsync(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect.!!!!"); return; } RADBContext db = new RADBContext(); var userRole = user.Roles.FirstOrDefault(); var role = db.Roles.SingleOrDefault(r => r.Id == userRole.RoleId); var roleName = role?.Name; if (roleName == "Admin") { context.OwinContext.Response.Headers.Add("Role", new[] { "Admin" }); } else if (roleName == "Manager") { context.OwinContext.Response.Headers.Add("Role", new[] { "Manager" }); } else { context.OwinContext.Response.Headers.Add("Role", new[] { "User" }); } //Mora se dodati u header response-a kako bi se se Role atribut //mogao procitati na klijentskoj strani context.OwinContext.Response.Headers.Add("Access-Control-Expose-Headers", new[] { "Role" }); //if (!user.EmailConfirmed) //{ // context.SetError("invalid_grant", "AppUser did not confirm email."); // return; //} ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT"); var ticket = new AuthenticationTicket(oAuthIdentity, null); context.Validated(ticket); }