/// <summary> /// Called when a request to the Token endpoint arrives with a "grant_type" of "password". /// This occurs when the user has provided name and password credentials directly into the /// client application's user interface, and the client application is using those /// to acquire an "access_token" and optional "refresh_token" /// </summary> /// <param name="context">Context information</param> /// <returns>Task to enable asynchronous execution</returns> public override async Task GrantResourceOwnerCredentials( OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); using (IAuthenticationService authService = AuthenticationServiceProvider.GetAuthenticationService("NOV")) { bool authResult = authService.Authenticate(context.UserName, context.Password); if (authResult == false) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } } string usernameWithoutDomain = context.UserName.Substring( context.UserName.LastIndexOf(@"\") + 1); string fullName; using (IUserManager userMgr = UserManagerProvider.GetUserManager("NOV")) { fullName = userMgr.GetFullName(usernameWithoutDomain); } List <string> roles; using (IRoleManager roleManager = RoleManagerProvider.GetRoleManager("NOV")) { roles = roleManager.GetUserRoles(usernameWithoutDomain); } var oAuthIdentity = new ClaimsIdentity(context.Options.AuthenticationType); oAuthIdentity.AddClaim(new Claim(ClaimTypes.Name, usernameWithoutDomain)); foreach (var role in roles) { oAuthIdentity.AddClaim(new Claim(ClaimTypes.Role, role)); } AuthenticationProperties properties = CreateProperties(usernameWithoutDomain, fullName, roles); AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); context.Validated(ticket); }
private IUserService GetUserService() { var globalSettingsRepository = new GlobalSettingRepository(ConfigurationManager.ConnectionStrings["Unity"].ConnectionString); var applicationUserRepository = new ApplicationUserRepository(ConfigurationManager.ConnectionStrings["Unity"].ConnectionString); var manCoRepository = new ManCoRepository(ConfigurationManager.ConnectionStrings["Unity"].ConnectionString); var passwordHistoryRepository = new PasswordHistoryRepository(ConfigurationManager.ConnectionStrings["Unity"].ConnectionString); var userManagerProvider = new UserManagerProvider(ConfigurationManager.ConnectionStrings["Unity"].ConnectionString); var roleManagerProvider = new RoleManagerProvider(ConfigurationManager.ConnectionStrings["Unity"].ConnectionString); var authenticationManagerProvider = new AuthenticationManagerProvider(new HttpContextBaseProvider()); var sessionRepository = new SessionRepository(ConfigurationManager.ConnectionStrings["Unity"].ConnectionString); return(new UserService( userManagerProvider, roleManagerProvider, authenticationManagerProvider, applicationUserRepository, passwordHistoryRepository, globalSettingsRepository, manCoRepository, sessionRepository)); }