public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var             userManager = context.OwinContext.GetUserManager <ApplicationUserManager>();
            ApplicationUser user        = await userManager.FindAsync(context.UserName, context.Password);

            if (user == null)
            {
                //Sap Login
                var login = new LoginRequest
                {
                    LanguageSpecified     = true,
                    CompanyPassword       = context.Password,
                    CompanyUsername       = context.UserName,
                    DatabaseName          = Configuration.CompanyDB,
                    DatabaseServer        = Configuration.SapServer,
                    DatabaseType          = true,
                    DatabaseTypeSpecified = true,
                    Language          = LoginLanguage.ln_Turkish_Tr,
                    LicenseServer     = Configuration.SapLinsansServer,
                    LoginDatabaseType = LoginDatabaseType.dst_MSSQL2014
                };
                var sapuser = await _userEngine.LoginAsync(login);

                if (sapuser != "")
                {
                    var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                    identity.AddClaim(new Claim(ClaimTypes.Role, "User"));
                    identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
                    identity.AddClaim(new Claim(ClaimTypes.SerialNumber, sapuser));
                    AuthenticationProperties properties = CreateProperties(context.UserName);
                    AuthenticationTicket     ticket     = new AuthenticationTicket(identity, properties);
                    context.Validated(ticket);
                }
                else
                {
                    context.SetError("invalid_grant", "The user name or password is incorrect.");
                    return;
                }
            }
            else
            {
                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);
            }
        }
示例#2
0
 public Task <string> Login(LoginRequest request)
 {
     return(_userEngine.LoginAsync(request));
 }