/// <summary> /// 新建 /// </summary> /// <param name="endpoint"></param> /// <returns></returns> public async Task Add(ClientSystemLoginEndpoint endpoint) { await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, _dbConnectionFactory.CreateAllForSystemToken(), async (conn, transaction) => { SqlTransaction sqlTran = null; if (transaction != null) { sqlTran = (SqlTransaction)transaction; } await using (SqlCommand command = new SqlCommand() { Connection = (SqlConnection)conn, CommandType = CommandType.Text, Transaction = sqlTran, }) { SqlParameter parameter; if (endpoint.ID == Guid.Empty) { command.CommandText = @"INSERT INTO [dbo].[ClientSystemLoginEndpoint] ([id] ,[name] ,[signaturekey] ,[createtime] ,[modifytime]) VALUES (DEFAULT ,@name ,@signaturekey ,GETUTCDATE() ,GETUTCDATE()); SELECT @newid = id FROM [dbo].[ClientSystemLoginEndpoint] WHERE [sequence] = SCOPE_IDENTITY();"; parameter = new SqlParameter("@newid", SqlDbType.UniqueIdentifier) { Direction = ParameterDirection.Output }; command.Parameters.Add(parameter); } else { command.CommandText = @"INSERT INTO [dbo].[ClientSystemLoginEndpoint] ([id] ,[name] ,[signaturekey] ,[createtime] ,[modifytime]) VALUES (@id ,@name ,@signaturekey ,GETUTCDATE() ,GETUTCDATE());"; parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier) { Value = endpoint.ID }; command.Parameters.Add(parameter); } parameter = new SqlParameter("@name", SqlDbType.NVarChar, 500) { Value = endpoint.Name }; command.Parameters.Add(parameter); parameter = new SqlParameter("@signaturekey", SqlDbType.NVarChar, 150) { Value = endpoint.SignatureKey }; command.Parameters.Add(parameter); await command.PrepareAsync(); try { await command.ExecuteNonQueryAsync(); } catch (SqlException ex) { if (ex == null) { throw; } if (ex.Number == 2601) { var fragment = new TextFragment() { Code = TextCodes.ExistClientSystemLoginEndpointByName, DefaultFormatting = "客户端系统登陆终结点中存在相同的名称\"{0}\"数据", ReplaceParameters = new List <object>() { endpoint.Name } }; throw new UtilityException((int)Errors.ExistClientSystemLoginEndpointByName, fragment); } else { throw; } } if (endpoint.ID == Guid.Empty) { endpoint.ID = (Guid)command.Parameters["@newid"].Value; } ; } }); }
/// <summary> /// 新建 /// </summary> /// <param name="endpoint"></param> /// <returns></returns> public async Task Add(AuthorizationEndpoint endpoint) { await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, _dbConnectionFactory.CreateAllForSystemToken(), async (conn, transaction) => { SqlTransaction sqlTran = null; if (transaction != null) { sqlTran = (SqlTransaction)transaction; } await using (SqlCommand command = new SqlCommand() { Connection = (SqlConnection)conn, CommandType = CommandType.Text, Transaction = sqlTran, }) { SqlParameter parameter; if (endpoint.ID == Guid.Empty) { command.CommandText = @"insert into [dbo].[AuthorizationEndpoint] ( [id] ,[name] ,[thirdpartytype] ,[thirdpartypostexecutetype] ,[thirdpartyconfiguration] ,[thirdpartypostconfiguration] ,[keepthirdpartytoken] ,[authmodes] ,[createtime] ,[modifytime] ) values ( default ,@name ,@thirdpartytype ,@thirdpartypostexecutetype ,@thirdpartyconfiguration ,@thirdpartypostconfiguration ,@keepthirdpartytoken ,@authmodes ,getutcdate() ,getutcdate() ) select @newid =[id] from [dbo].[AuthorizationEndpoint] where [sequence] = SCOPE_IDENTITY()"; parameter = new SqlParameter("@newid", SqlDbType.UniqueIdentifier) { Direction = ParameterDirection.Output }; command.Parameters.Add(parameter); } else { command.CommandText = @"insert into [dbo].[AuthorizationEndpoint] ( [id] ,[name] ,[thirdpartytype] ,[thirdpartypostexecutetype] ,[thirdpartyconfiguration] ,[thirdpartypostconfiguration] ,[keepthirdpartytoken] ,[authmodes] ,[createtime] ,[modifytime] ) values ( @id ,@name ,@thirdpartytype ,@thirdpartypostexecutetype ,@thirdpartyconfiguration ,@thirdpartypostconfiguration ,@keepthirdpartytoken ,@authmodes ,getutcdate() ,getutcdate() ) "; parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier) { Value = endpoint.ID }; command.Parameters.Add(parameter); } parameter = new SqlParameter("@name", SqlDbType.VarChar, 150) { Value = endpoint.Name }; command.Parameters.Add(parameter); parameter = new SqlParameter("@thirdpartytype", SqlDbType.VarChar, 150) { Value = endpoint.ThirdPartyType }; command.Parameters.Add(parameter); object thirdPartyPostExecuteType = DBNull.Value; if (endpoint.ThirdPartyPostExecuteType != null) { thirdPartyPostExecuteType = endpoint.ThirdPartyPostExecuteType; } parameter = new SqlParameter("@thirdpartypostexecutetype", SqlDbType.VarChar, 150) { Value = thirdPartyPostExecuteType }; command.Parameters.Add(parameter); parameter = new SqlParameter("@thirdpartyconfiguration", SqlDbType.NVarChar, endpoint.ThirdPartyConfiguration.Length) { Value = endpoint.ThirdPartyConfiguration }; command.Parameters.Add(parameter); object thirdPartyPostConfiguration = DBNull.Value; int thirdPartyPostConfigurationLength = 100; if (endpoint.ThirdPartyPostConfiguration != null) { thirdPartyPostConfiguration = endpoint.ThirdPartyPostConfiguration; thirdPartyPostConfigurationLength = endpoint.ThirdPartyPostConfiguration.Length; } parameter = new SqlParameter("@thirdpartypostconfiguration", SqlDbType.NVarChar, thirdPartyPostConfigurationLength) { Value = thirdPartyPostConfiguration }; command.Parameters.Add(parameter); parameter = new SqlParameter("@keepthirdpartytoken", SqlDbType.Bit) { Value = endpoint.KeepThirdPartyToken }; command.Parameters.Add(parameter); var strAuthmodes = await endpoint.AuthModes.ToDisplayString(async(item) => await Task.FromResult(item.ToString()), async() => await Task.FromResult(",")); parameter = new SqlParameter("@authmodes", SqlDbType.VarChar, strAuthmodes.Length) { Value = strAuthmodes }; command.Parameters.Add(parameter); await command.PrepareAsync(); try { await command.ExecuteNonQueryAsync(); } catch (SqlException ex) { if (ex == null) { throw; } //违反主键约束 if (ex.Number == 2601) { var fragment = new TextFragment() { Code = TextCodes.ExistAuthorizationEndpointByName, DefaultFormatting = "验证终结点数据中存在相同名称\"{0}\"数据", ReplaceParameters = new List <object>() { endpoint.Name } }; throw new UtilityException((int)Errors.ExistAuthorizationEndpointByName, fragment); } else { throw; } } //如果用户未赋值ID则创建成功后返回ID if (endpoint.ID == Guid.Empty) { endpoint.ID = (Guid)command.Parameters["@newid"].Value; } ; } }); }
/// <summary> /// 新建 /// </summary> /// <param name="endpoint"></param> /// <returns></returns> public async Task Add(SystemLoginEndpoint endpoint) { await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, _dbConnectionFactory.CreateAllForSystemToken(), async (conn, transaction) => { SqlTransaction sqlTran = null; if (sqlTran != null) { sqlTran = (SqlTransaction)transaction; } await using (SqlCommand command = new SqlCommand() { Connection = (SqlConnection)conn, CommandType = CommandType.Text, Transaction = sqlTran }) { SqlParameter parameter; if (endpoint.ID == Guid.Empty) { command.CommandText = @"INSERT INTO [dbo].[SystemLoginEndpoint] ( [id], [name], [secretkey], [expiresecond], [clientredirectbaseurls], [baseurl], [userinfokey], [createtime], [modifytime] ) VALUES (DEFAULT, @name, @secretkey, @expiresecond, @clientredirectbaseurls, @baseurl, @userinfokey, GETUTCDATE(), GETUTCDATE()); SELECT @newid = id FROM [dbo].[SystemLoginEndpoint] WHERE [sequence] = SCOPE_IDENTITY();"; parameter = new SqlParameter("@newid", SqlDbType.UniqueIdentifier) { Direction = ParameterDirection.Output }; command.Parameters.Add(parameter); } else { command.CommandText = @"INSERT INTO [dbo].[SystemLoginEndpoint] ( [id], [name], [secretkey], [expiresecond], [clientredirectbaseurls], [baseurl], [userinfokey], [createtime], [modifytime] ) VALUES (@id, @name, @secretkey, @expiresecond, @clientredirectbaseurls, @baseurl, @userinfokey, GETUTCDATE(), GETUTCDATE());"; parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier) { Value = endpoint.ID }; command.Parameters.Add(parameter); } parameter = new SqlParameter("@name", SqlDbType.VarChar, 150) { Value = endpoint.Name }; command.Parameters.Add(parameter); parameter = new SqlParameter("@secretkey", SqlDbType.VarChar, 200) { Value = endpoint.SecretKey }; command.Parameters.Add(parameter); parameter = new SqlParameter("@expiresecond", SqlDbType.Int) { Value = endpoint.ExpireSecond }; command.Parameters.Add(parameter); if (endpoint.ClientRedirectBaseUrls != null) { StringBuilder sUrl = new StringBuilder(); for (int i = 0; i < endpoint.ClientRedirectBaseUrls.Length; i++) { sUrl.Append(endpoint.ClientRedirectBaseUrls[i] + ","); } ; parameter = new SqlParameter("@clientredirectbaseurls", SqlDbType.VarChar, sUrl.Length) { Value = sUrl.ToString().TrimEnd(',') }; command.Parameters.Add(parameter); } else { parameter = new SqlParameter("@clientredirectbaseurls", SqlDbType.VarChar, 1000) { Value = DBNull.Value }; command.Parameters.Add(parameter); } parameter = new SqlParameter("@baseurl", SqlDbType.VarChar, 500) { Value = endpoint.BaseUrl }; command.Parameters.Add(parameter); parameter = new SqlParameter("@userinfokey", SqlDbType.VarChar, 150) { Value = endpoint.UserInfoKey }; command.Parameters.Add(parameter); await command.PrepareAsync(); try { await command.ExecuteNonQueryAsync(); } catch (SqlException ex) { if (ex == null) { throw; } if (ex.Number == 2601) { var fragment = new TextFragment() { Code = TextCodes.ExistSystemLoginEndpointByName, DefaultFormatting = "系统登录终结点中已存在相同名称\"{0}\"数据", ReplaceParameters = new List <object>() { endpoint.Name } }; throw new UtilityException((int)Errors.ExistSystemLoginEndpointByName, fragment); } else { throw; } } if (endpoint.ID == Guid.Empty) { endpoint.ID = (Guid)command.Parameters["@newid"].Value; } ; } }); }